at_hdmac.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for the Atmel AHB DMA Controller (aka HDMA or DMAC on AT91 systems)
  4. *
  5. * Copyright (C) 2008 Atmel Corporation
  6. * Copyright (C) 2022 Microchip Technology, Inc. and its subsidiaries
  7. *
  8. * This supports the Atmel AHB DMA Controller found in several Atmel SoCs.
  9. * The only Atmel DMA Controller that is not covered by this driver is the one
  10. * found on AT91SAM9263.
  11. */
  12. #include <dt-bindings/dma/at91.h>
  13. #include <linux/bitfield.h>
  14. #include <linux/clk.h>
  15. #include <linux/dmaengine.h>
  16. #include <linux/dmapool.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/overflow.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/of_dma.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/slab.h>
  26. #include "dmaengine.h"
  27. #include "virt-dma.h"
  28. /*
  29. * Glossary
  30. * --------
  31. *
  32. * at_hdmac : Name of the ATmel AHB DMA Controller
  33. * at_dma_ / atdma : ATmel DMA controller entity related
  34. * atc_ / atchan : ATmel DMA Channel entity related
  35. */
  36. #define AT_DMA_MAX_NR_CHANNELS 8
  37. /* Global Configuration Register */
  38. #define AT_DMA_GCFG 0x00
  39. #define AT_DMA_IF_BIGEND(i) BIT((i)) /* AHB-Lite Interface i in Big-endian mode */
  40. #define AT_DMA_ARB_CFG BIT(4) /* Arbiter mode. */
  41. /* Controller Enable Register */
  42. #define AT_DMA_EN 0x04
  43. #define AT_DMA_ENABLE BIT(0)
  44. /* Software Single Request Register */
  45. #define AT_DMA_SREQ 0x08
  46. #define AT_DMA_SSREQ(x) BIT((x) << 1) /* Request a source single transfer on channel x */
  47. #define AT_DMA_DSREQ(x) BIT(1 + ((x) << 1)) /* Request a destination single transfer on channel x */
  48. /* Software Chunk Transfer Request Register */
  49. #define AT_DMA_CREQ 0x0c
  50. #define AT_DMA_SCREQ(x) BIT((x) << 1) /* Request a source chunk transfer on channel x */
  51. #define AT_DMA_DCREQ(x) BIT(1 + ((x) << 1)) /* Request a destination chunk transfer on channel x */
  52. /* Software Last Transfer Flag Register */
  53. #define AT_DMA_LAST 0x10
  54. #define AT_DMA_SLAST(x) BIT((x) << 1) /* This src rq is last tx of buffer on channel x */
  55. #define AT_DMA_DLAST(x) BIT(1 + ((x) << 1)) /* This dst rq is last tx of buffer on channel x */
  56. /* Request Synchronization Register */
  57. #define AT_DMA_SYNC 0x14
  58. #define AT_DMA_SYR(h) BIT((h)) /* Synchronize handshake line h */
  59. /* Error, Chained Buffer transfer completed and Buffer transfer completed Interrupt registers */
  60. #define AT_DMA_EBCIER 0x18 /* Enable register */
  61. #define AT_DMA_EBCIDR 0x1c /* Disable register */
  62. #define AT_DMA_EBCIMR 0x20 /* Mask Register */
  63. #define AT_DMA_EBCISR 0x24 /* Status Register */
  64. #define AT_DMA_CBTC_OFFSET 8
  65. #define AT_DMA_ERR_OFFSET 16
  66. #define AT_DMA_BTC(x) BIT((x))
  67. #define AT_DMA_CBTC(x) BIT(AT_DMA_CBTC_OFFSET + (x))
  68. #define AT_DMA_ERR(x) BIT(AT_DMA_ERR_OFFSET + (x))
  69. /* Channel Handler Enable Register */
  70. #define AT_DMA_CHER 0x28
  71. #define AT_DMA_ENA(x) BIT((x))
  72. #define AT_DMA_SUSP(x) BIT(8 + (x))
  73. #define AT_DMA_KEEP(x) BIT(24 + (x))
  74. /* Channel Handler Disable Register */
  75. #define AT_DMA_CHDR 0x2c
  76. #define AT_DMA_DIS(x) BIT(x)
  77. #define AT_DMA_RES(x) BIT(8 + (x))
  78. /* Channel Handler Status Register */
  79. #define AT_DMA_CHSR 0x30
  80. #define AT_DMA_EMPT(x) BIT(16 + (x))
  81. #define AT_DMA_STAL(x) BIT(24 + (x))
  82. /* Channel registers base address */
  83. #define AT_DMA_CH_REGS_BASE 0x3c
  84. #define ch_regs(x) (AT_DMA_CH_REGS_BASE + (x) * 0x28) /* Channel x base addr */
  85. /* Hardware register offset for each channel */
  86. #define ATC_SADDR_OFFSET 0x00 /* Source Address Register */
  87. #define ATC_DADDR_OFFSET 0x04 /* Destination Address Register */
  88. #define ATC_DSCR_OFFSET 0x08 /* Descriptor Address Register */
  89. #define ATC_CTRLA_OFFSET 0x0c /* Control A Register */
  90. #define ATC_CTRLB_OFFSET 0x10 /* Control B Register */
  91. #define ATC_CFG_OFFSET 0x14 /* Configuration Register */
  92. #define ATC_SPIP_OFFSET 0x18 /* Src PIP Configuration Register */
  93. #define ATC_DPIP_OFFSET 0x1c /* Dst PIP Configuration Register */
  94. /* Bitfield definitions */
  95. /* Bitfields in DSCR */
  96. #define ATC_DSCR_IF GENMASK(1, 0) /* Dsc feched via AHB-Lite Interface */
  97. /* Bitfields in CTRLA */
  98. #define ATC_BTSIZE_MAX GENMASK(15, 0) /* Maximum Buffer Transfer Size */
  99. #define ATC_BTSIZE GENMASK(15, 0) /* Buffer Transfer Size */
  100. #define ATC_SCSIZE GENMASK(18, 16) /* Source Chunk Transfer Size */
  101. #define ATC_DCSIZE GENMASK(22, 20) /* Destination Chunk Transfer Size */
  102. #define ATC_SRC_WIDTH GENMASK(25, 24) /* Source Single Transfer Size */
  103. #define ATC_DST_WIDTH GENMASK(29, 28) /* Destination Single Transfer Size */
  104. #define ATC_DONE BIT(31) /* Tx Done (only written back in descriptor) */
  105. /* Bitfields in CTRLB */
  106. #define ATC_SIF GENMASK(1, 0) /* Src tx done via AHB-Lite Interface i */
  107. #define ATC_DIF GENMASK(5, 4) /* Dst tx done via AHB-Lite Interface i */
  108. #define AT_DMA_MEM_IF 0x0 /* interface 0 as memory interface */
  109. #define AT_DMA_PER_IF 0x1 /* interface 1 as peripheral interface */
  110. #define ATC_SRC_PIP BIT(8) /* Source Picture-in-Picture enabled */
  111. #define ATC_DST_PIP BIT(12) /* Destination Picture-in-Picture enabled */
  112. #define ATC_SRC_DSCR_DIS BIT(16) /* Src Descriptor fetch disable */
  113. #define ATC_DST_DSCR_DIS BIT(20) /* Dst Descriptor fetch disable */
  114. #define ATC_FC GENMASK(23, 21) /* Choose Flow Controller */
  115. #define ATC_FC_MEM2MEM 0x0 /* Mem-to-Mem (DMA) */
  116. #define ATC_FC_MEM2PER 0x1 /* Mem-to-Periph (DMA) */
  117. #define ATC_FC_PER2MEM 0x2 /* Periph-to-Mem (DMA) */
  118. #define ATC_FC_PER2PER 0x3 /* Periph-to-Periph (DMA) */
  119. #define ATC_FC_PER2MEM_PER 0x4 /* Periph-to-Mem (Peripheral) */
  120. #define ATC_FC_MEM2PER_PER 0x5 /* Mem-to-Periph (Peripheral) */
  121. #define ATC_FC_PER2PER_SRCPER 0x6 /* Periph-to-Periph (Src Peripheral) */
  122. #define ATC_FC_PER2PER_DSTPER 0x7 /* Periph-to-Periph (Dst Peripheral) */
  123. #define ATC_SRC_ADDR_MODE GENMASK(25, 24)
  124. #define ATC_SRC_ADDR_MODE_INCR 0x0 /* Incrementing Mode */
  125. #define ATC_SRC_ADDR_MODE_DECR 0x1 /* Decrementing Mode */
  126. #define ATC_SRC_ADDR_MODE_FIXED 0x2 /* Fixed Mode */
  127. #define ATC_DST_ADDR_MODE GENMASK(29, 28)
  128. #define ATC_DST_ADDR_MODE_INCR 0x0 /* Incrementing Mode */
  129. #define ATC_DST_ADDR_MODE_DECR 0x1 /* Decrementing Mode */
  130. #define ATC_DST_ADDR_MODE_FIXED 0x2 /* Fixed Mode */
  131. #define ATC_IEN BIT(30) /* BTC interrupt enable (active low) */
  132. #define ATC_AUTO BIT(31) /* Auto multiple buffer tx enable */
  133. /* Bitfields in CFG */
  134. #define ATC_SRC_PER GENMASK(3, 0) /* Channel src rq associated with periph handshaking ifc h */
  135. #define ATC_DST_PER GENMASK(7, 4) /* Channel dst rq associated with periph handshaking ifc h */
  136. #define ATC_SRC_REP BIT(8) /* Source Replay Mod */
  137. #define ATC_SRC_H2SEL BIT(9) /* Source Handshaking Mod */
  138. #define ATC_SRC_PER_MSB GENMASK(11, 10) /* Channel src rq (most significant bits) */
  139. #define ATC_DST_REP BIT(12) /* Destination Replay Mod */
  140. #define ATC_DST_H2SEL BIT(13) /* Destination Handshaking Mod */
  141. #define ATC_DST_PER_MSB GENMASK(15, 14) /* Channel dst rq (most significant bits) */
  142. #define ATC_SOD BIT(16) /* Stop On Done */
  143. #define ATC_LOCK_IF BIT(20) /* Interface Lock */
  144. #define ATC_LOCK_B BIT(21) /* AHB Bus Lock */
  145. #define ATC_LOCK_IF_L BIT(22) /* Master Interface Arbiter Lock */
  146. #define ATC_AHB_PROT GENMASK(26, 24) /* AHB Protection */
  147. #define ATC_FIFOCFG GENMASK(29, 28) /* FIFO Request Configuration */
  148. #define ATC_FIFOCFG_LARGESTBURST 0x0
  149. #define ATC_FIFOCFG_HALFFIFO 0x1
  150. #define ATC_FIFOCFG_ENOUGHSPACE 0x2
  151. /* Bitfields in SPIP */
  152. #define ATC_SPIP_HOLE GENMASK(15, 0)
  153. #define ATC_SPIP_BOUNDARY GENMASK(25, 16)
  154. /* Bitfields in DPIP */
  155. #define ATC_DPIP_HOLE GENMASK(15, 0)
  156. #define ATC_DPIP_BOUNDARY GENMASK(25, 16)
  157. #define ATC_PER_MSB GENMASK(5, 4) /* Extract MSBs of a handshaking identifier */
  158. #define ATC_SRC_PER_ID(id) \
  159. ({ typeof(id) _id = (id); \
  160. FIELD_PREP(ATC_SRC_PER_MSB, FIELD_GET(ATC_PER_MSB, _id)) | \
  161. FIELD_PREP(ATC_SRC_PER, _id); })
  162. #define ATC_DST_PER_ID(id) \
  163. ({ typeof(id) _id = (id); \
  164. FIELD_PREP(ATC_DST_PER_MSB, FIELD_GET(ATC_PER_MSB, _id)) | \
  165. FIELD_PREP(ATC_DST_PER, _id); })
  166. /*-- descriptors -----------------------------------------------------*/
  167. /* LLI == Linked List Item; aka DMA buffer descriptor */
  168. struct at_lli {
  169. /* values that are not changed by hardware */
  170. u32 saddr;
  171. u32 daddr;
  172. /* value that may get written back: */
  173. u32 ctrla;
  174. /* more values that are not changed by hardware */
  175. u32 ctrlb;
  176. u32 dscr; /* chain to next lli */
  177. };
  178. /**
  179. * struct atdma_sg - atdma scatter gather entry
  180. * @len: length of the current Linked List Item.
  181. * @lli: linked list item that is passed to the DMA controller
  182. * @lli_phys: physical address of the LLI.
  183. */
  184. struct atdma_sg {
  185. unsigned int len;
  186. struct at_lli *lli;
  187. dma_addr_t lli_phys;
  188. };
  189. /**
  190. * struct at_desc - software descriptor
  191. * @vd: pointer to the virtual dma descriptor.
  192. * @atchan: pointer to the atmel dma channel.
  193. * @total_len: total transaction byte count
  194. * @sglen: number of sg entries.
  195. * @sg: array of sgs.
  196. * @boundary: number of transfers to perform before the automatic address increment operation
  197. * @dst_hole: value to add to the destination address when the boundary has been reached
  198. * @src_hole: value to add to the source address when the boundary has been reached
  199. * @memset_buffer: buffer used for the memset operation
  200. * @memset_paddr: physical address of the buffer used for the memset operation
  201. * @memset_vaddr: virtual address of the buffer used for the memset operation
  202. */
  203. struct at_desc {
  204. struct virt_dma_desc vd;
  205. struct at_dma_chan *atchan;
  206. size_t total_len;
  207. unsigned int sglen;
  208. /* Interleaved data */
  209. size_t boundary;
  210. size_t dst_hole;
  211. size_t src_hole;
  212. /* Memset temporary buffer */
  213. bool memset_buffer;
  214. dma_addr_t memset_paddr;
  215. int *memset_vaddr;
  216. struct atdma_sg sg[] __counted_by(sglen);
  217. };
  218. /*-- Channels --------------------------------------------------------*/
  219. /**
  220. * enum atc_status - information bits stored in channel status flag
  221. *
  222. * @ATC_IS_PAUSED: If channel is pauses
  223. * @ATC_IS_CYCLIC: If channel is cyclic
  224. *
  225. * Manipulated with atomic operations.
  226. */
  227. enum atc_status {
  228. ATC_IS_PAUSED = 1,
  229. ATC_IS_CYCLIC = 24,
  230. };
  231. /**
  232. * struct at_dma_chan - internal representation of an Atmel HDMAC channel
  233. * @vc: virtual dma channel entry.
  234. * @atdma: pointer to the driver data.
  235. * @ch_regs: memory mapped register base
  236. * @mask: channel index in a mask
  237. * @per_if: peripheral interface
  238. * @mem_if: memory interface
  239. * @status: transmit status information from irq/prep* functions
  240. * to tasklet (use atomic operations)
  241. * @save_cfg: configuration register that is saved on suspend/resume cycle
  242. * @save_dscr: for cyclic operations, preserve next descriptor address in
  243. * the cyclic list on suspend/resume cycle
  244. * @dma_sconfig: configuration for slave transfers, passed via
  245. * .device_config
  246. * @desc: pointer to the atmel dma descriptor.
  247. */
  248. struct at_dma_chan {
  249. struct virt_dma_chan vc;
  250. struct at_dma *atdma;
  251. void __iomem *ch_regs;
  252. u8 mask;
  253. u8 per_if;
  254. u8 mem_if;
  255. unsigned long status;
  256. u32 save_cfg;
  257. u32 save_dscr;
  258. struct dma_slave_config dma_sconfig;
  259. struct at_desc *desc;
  260. };
  261. #define channel_readl(atchan, name) \
  262. __raw_readl((atchan)->ch_regs + ATC_##name##_OFFSET)
  263. #define channel_writel(atchan, name, val) \
  264. __raw_writel((val), (atchan)->ch_regs + ATC_##name##_OFFSET)
  265. /*
  266. * Fix sconfig's burst size according to at_hdmac. We need to convert them as:
  267. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3, 32 -> 4, 64 -> 5, 128 -> 6, 256 -> 7.
  268. *
  269. * This can be done by finding most significant bit set.
  270. */
  271. static inline void convert_burst(u32 *maxburst)
  272. {
  273. if (*maxburst > 1)
  274. *maxburst = fls(*maxburst) - 2;
  275. else
  276. *maxburst = 0;
  277. }
  278. /*
  279. * Fix sconfig's bus width according to at_hdmac.
  280. * 1 byte -> 0, 2 bytes -> 1, 4 bytes -> 2.
  281. */
  282. static inline u8 convert_buswidth(enum dma_slave_buswidth addr_width)
  283. {
  284. switch (addr_width) {
  285. case DMA_SLAVE_BUSWIDTH_2_BYTES:
  286. return 1;
  287. case DMA_SLAVE_BUSWIDTH_4_BYTES:
  288. return 2;
  289. default:
  290. /* For 1 byte width or fallback */
  291. return 0;
  292. }
  293. }
  294. /*-- Controller ------------------------------------------------------*/
  295. /**
  296. * struct at_dma - internal representation of an Atmel HDMA Controller
  297. * @dma_device: dmaengine dma_device object members
  298. * @regs: memory mapped register base
  299. * @clk: dma controller clock
  300. * @save_imr: interrupt mask register that is saved on suspend/resume cycle
  301. * @all_chan_mask: all channels available in a mask
  302. * @lli_pool: hw lli table
  303. * @memset_pool: hw memset pool
  304. * @chan: channels table to store at_dma_chan structures
  305. */
  306. struct at_dma {
  307. struct dma_device dma_device;
  308. void __iomem *regs;
  309. struct clk *clk;
  310. u32 save_imr;
  311. u8 all_chan_mask;
  312. struct dma_pool *lli_pool;
  313. struct dma_pool *memset_pool;
  314. /* AT THE END channels table */
  315. struct at_dma_chan chan[];
  316. };
  317. #define dma_readl(atdma, name) \
  318. __raw_readl((atdma)->regs + AT_DMA_##name)
  319. #define dma_writel(atdma, name, val) \
  320. __raw_writel((val), (atdma)->regs + AT_DMA_##name)
  321. static inline struct at_desc *to_atdma_desc(struct dma_async_tx_descriptor *t)
  322. {
  323. return container_of(t, struct at_desc, vd.tx);
  324. }
  325. static inline struct at_dma_chan *to_at_dma_chan(struct dma_chan *chan)
  326. {
  327. return container_of(chan, struct at_dma_chan, vc.chan);
  328. }
  329. static inline struct at_dma *to_at_dma(struct dma_device *ddev)
  330. {
  331. return container_of(ddev, struct at_dma, dma_device);
  332. }
  333. /*-- Helper functions ------------------------------------------------*/
  334. static struct device *chan2dev(struct dma_chan *chan)
  335. {
  336. return &chan->dev->device;
  337. }
  338. #if defined(VERBOSE_DEBUG)
  339. static void vdbg_dump_regs(struct at_dma_chan *atchan)
  340. {
  341. struct at_dma *atdma = to_at_dma(atchan->vc.chan.device);
  342. dev_err(chan2dev(&atchan->vc.chan),
  343. " channel %d : imr = 0x%x, chsr = 0x%x\n",
  344. atchan->vc.chan.chan_id,
  345. dma_readl(atdma, EBCIMR),
  346. dma_readl(atdma, CHSR));
  347. dev_err(chan2dev(&atchan->vc.chan),
  348. " channel: s0x%x d0x%x ctrl0x%x:0x%x cfg0x%x l0x%x\n",
  349. channel_readl(atchan, SADDR),
  350. channel_readl(atchan, DADDR),
  351. channel_readl(atchan, CTRLA),
  352. channel_readl(atchan, CTRLB),
  353. channel_readl(atchan, CFG),
  354. channel_readl(atchan, DSCR));
  355. }
  356. #else
  357. static void vdbg_dump_regs(struct at_dma_chan *atchan) {}
  358. #endif
  359. static void atc_dump_lli(struct at_dma_chan *atchan, struct at_lli *lli)
  360. {
  361. dev_crit(chan2dev(&atchan->vc.chan),
  362. "desc: s%pad d%pad ctrl0x%x:0x%x l%pad\n",
  363. &lli->saddr, &lli->daddr,
  364. lli->ctrla, lli->ctrlb, &lli->dscr);
  365. }
  366. static void atc_setup_irq(struct at_dma *atdma, int chan_id, int on)
  367. {
  368. u32 ebci;
  369. /* enable interrupts on buffer transfer completion & error */
  370. ebci = AT_DMA_BTC(chan_id)
  371. | AT_DMA_ERR(chan_id);
  372. if (on)
  373. dma_writel(atdma, EBCIER, ebci);
  374. else
  375. dma_writel(atdma, EBCIDR, ebci);
  376. }
  377. static void atc_enable_chan_irq(struct at_dma *atdma, int chan_id)
  378. {
  379. atc_setup_irq(atdma, chan_id, 1);
  380. }
  381. static void atc_disable_chan_irq(struct at_dma *atdma, int chan_id)
  382. {
  383. atc_setup_irq(atdma, chan_id, 0);
  384. }
  385. /**
  386. * atc_chan_is_enabled - test if given channel is enabled
  387. * @atchan: channel we want to test status
  388. */
  389. static inline int atc_chan_is_enabled(struct at_dma_chan *atchan)
  390. {
  391. struct at_dma *atdma = to_at_dma(atchan->vc.chan.device);
  392. return !!(dma_readl(atdma, CHSR) & atchan->mask);
  393. }
  394. /**
  395. * atc_chan_is_paused - test channel pause/resume status
  396. * @atchan: channel we want to test status
  397. */
  398. static inline int atc_chan_is_paused(struct at_dma_chan *atchan)
  399. {
  400. return test_bit(ATC_IS_PAUSED, &atchan->status);
  401. }
  402. /**
  403. * atc_chan_is_cyclic - test if given channel has cyclic property set
  404. * @atchan: channel we want to test status
  405. */
  406. static inline int atc_chan_is_cyclic(struct at_dma_chan *atchan)
  407. {
  408. return test_bit(ATC_IS_CYCLIC, &atchan->status);
  409. }
  410. /**
  411. * set_lli_eol - set end-of-link to descriptor so it will end transfer
  412. * @desc: descriptor, signle or at the end of a chain, to end chain on
  413. * @i: index of the atmel scatter gather entry that is at the end of the chain.
  414. */
  415. static void set_lli_eol(struct at_desc *desc, unsigned int i)
  416. {
  417. u32 ctrlb = desc->sg[i].lli->ctrlb;
  418. ctrlb &= ~ATC_IEN;
  419. ctrlb |= ATC_SRC_DSCR_DIS | ATC_DST_DSCR_DIS;
  420. desc->sg[i].lli->ctrlb = ctrlb;
  421. desc->sg[i].lli->dscr = 0;
  422. }
  423. #define ATC_DEFAULT_CFG FIELD_PREP(ATC_FIFOCFG, ATC_FIFOCFG_HALFFIFO)
  424. #define ATC_DEFAULT_CTRLB (FIELD_PREP(ATC_SIF, AT_DMA_MEM_IF) | \
  425. FIELD_PREP(ATC_DIF, AT_DMA_MEM_IF))
  426. #define ATC_DMA_BUSWIDTHS\
  427. (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) |\
  428. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |\
  429. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |\
  430. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
  431. #define ATC_MAX_DSCR_TRIALS 10
  432. /*
  433. * Initial number of descriptors to allocate for each channel. This could
  434. * be increased during dma usage.
  435. */
  436. static unsigned int init_nr_desc_per_channel = 64;
  437. module_param(init_nr_desc_per_channel, uint, 0644);
  438. MODULE_PARM_DESC(init_nr_desc_per_channel,
  439. "initial descriptors per channel (default: 64)");
  440. /**
  441. * struct at_dma_platform_data - Controller configuration parameters
  442. * @nr_channels: Number of channels supported by hardware (max 8)
  443. * @cap_mask: dma_capability flags supported by the platform
  444. */
  445. struct at_dma_platform_data {
  446. unsigned int nr_channels;
  447. dma_cap_mask_t cap_mask;
  448. };
  449. /**
  450. * struct at_dma_slave - Controller-specific information about a slave
  451. * @dma_dev: required DMA master device
  452. * @cfg: Platform-specific initializer for the CFG register
  453. */
  454. struct at_dma_slave {
  455. struct device *dma_dev;
  456. u32 cfg;
  457. };
  458. static inline unsigned int atc_get_xfer_width(dma_addr_t src, dma_addr_t dst,
  459. size_t len)
  460. {
  461. unsigned int width;
  462. if (!((src | dst | len) & 3))
  463. width = 2;
  464. else if (!((src | dst | len) & 1))
  465. width = 1;
  466. else
  467. width = 0;
  468. return width;
  469. }
  470. static void atdma_lli_chain(struct at_desc *desc, unsigned int i)
  471. {
  472. struct atdma_sg *atdma_sg = &desc->sg[i];
  473. if (i)
  474. desc->sg[i - 1].lli->dscr = atdma_sg->lli_phys;
  475. }
  476. /**
  477. * atc_dostart - starts the DMA engine for real
  478. * @atchan: the channel we want to start
  479. */
  480. static void atc_dostart(struct at_dma_chan *atchan)
  481. {
  482. struct virt_dma_desc *vd = vchan_next_desc(&atchan->vc);
  483. struct at_desc *desc;
  484. if (!vd) {
  485. atchan->desc = NULL;
  486. return;
  487. }
  488. vdbg_dump_regs(atchan);
  489. list_del(&vd->node);
  490. atchan->desc = desc = to_atdma_desc(&vd->tx);
  491. channel_writel(atchan, SADDR, 0);
  492. channel_writel(atchan, DADDR, 0);
  493. channel_writel(atchan, CTRLA, 0);
  494. channel_writel(atchan, CTRLB, 0);
  495. channel_writel(atchan, DSCR, desc->sg[0].lli_phys);
  496. channel_writel(atchan, SPIP,
  497. FIELD_PREP(ATC_SPIP_HOLE, desc->src_hole) |
  498. FIELD_PREP(ATC_SPIP_BOUNDARY, desc->boundary));
  499. channel_writel(atchan, DPIP,
  500. FIELD_PREP(ATC_DPIP_HOLE, desc->dst_hole) |
  501. FIELD_PREP(ATC_DPIP_BOUNDARY, desc->boundary));
  502. /* Don't allow CPU to reorder channel enable. */
  503. wmb();
  504. dma_writel(atchan->atdma, CHER, atchan->mask);
  505. vdbg_dump_regs(atchan);
  506. }
  507. static void atdma_desc_free(struct virt_dma_desc *vd)
  508. {
  509. struct at_dma *atdma = to_at_dma(vd->tx.chan->device);
  510. struct at_desc *desc = to_atdma_desc(&vd->tx);
  511. unsigned int i;
  512. for (i = 0; i < desc->sglen; i++) {
  513. if (desc->sg[i].lli)
  514. dma_pool_free(atdma->lli_pool, desc->sg[i].lli,
  515. desc->sg[i].lli_phys);
  516. }
  517. /* If the transfer was a memset, free our temporary buffer */
  518. if (desc->memset_buffer) {
  519. dma_pool_free(atdma->memset_pool, desc->memset_vaddr,
  520. desc->memset_paddr);
  521. desc->memset_buffer = false;
  522. }
  523. kfree(desc);
  524. }
  525. /**
  526. * atc_calc_bytes_left - calculates the number of bytes left according to the
  527. * value read from CTRLA.
  528. *
  529. * @current_len: the number of bytes left before reading CTRLA
  530. * @ctrla: the value of CTRLA
  531. */
  532. static inline u32 atc_calc_bytes_left(u32 current_len, u32 ctrla)
  533. {
  534. u32 btsize = FIELD_GET(ATC_BTSIZE, ctrla);
  535. u32 src_width = FIELD_GET(ATC_SRC_WIDTH, ctrla);
  536. /*
  537. * According to the datasheet, when reading the Control A Register
  538. * (ctrla), the Buffer Transfer Size (btsize) bitfield refers to the
  539. * number of transfers completed on the Source Interface.
  540. * So btsize is always a number of source width transfers.
  541. */
  542. return current_len - (btsize << src_width);
  543. }
  544. /**
  545. * atc_get_llis_residue - Get residue for a hardware linked list transfer
  546. * @atchan: pointer to an atmel hdmac channel.
  547. * @desc: pointer to the descriptor for which the residue is calculated.
  548. * @residue: residue to be set to dma_tx_state.
  549. *
  550. * Calculate the residue by removing the length of the Linked List Item (LLI)
  551. * already transferred from the total length. To get the current LLI we can use
  552. * the value of the channel's DSCR register and compare it against the DSCR
  553. * value of each LLI.
  554. *
  555. * The CTRLA register provides us with the amount of data already read from the
  556. * source for the LLI. So we can compute a more accurate residue by also
  557. * removing the number of bytes corresponding to this amount of data.
  558. *
  559. * However, the DSCR and CTRLA registers cannot be read both atomically. Hence a
  560. * race condition may occur: the first read register may refer to one LLI
  561. * whereas the second read may refer to a later LLI in the list because of the
  562. * DMA transfer progression inbetween the two reads.
  563. *
  564. * One solution could have been to pause the DMA transfer, read the DSCR and
  565. * CTRLA then resume the DMA transfer. Nonetheless, this approach presents some
  566. * drawbacks:
  567. * - If the DMA transfer is paused, RX overruns or TX underruns are more likey
  568. * to occur depending on the system latency. Taking the USART driver as an
  569. * example, it uses a cyclic DMA transfer to read data from the Receive
  570. * Holding Register (RHR) to avoid RX overruns since the RHR is not protected
  571. * by any FIFO on most Atmel SoCs. So pausing the DMA transfer to compute the
  572. * residue would break the USART driver design.
  573. * - The atc_pause() function masks interrupts but we'd rather avoid to do so
  574. * for system latency purpose.
  575. *
  576. * Then we'd rather use another solution: the DSCR is read a first time, the
  577. * CTRLA is read in turn, next the DSCR is read a second time. If the two
  578. * consecutive read values of the DSCR are the same then we assume both refers
  579. * to the very same LLI as well as the CTRLA value read inbetween does. For
  580. * cyclic transfers, the assumption is that a full loop is "not so fast". If the
  581. * two DSCR values are different, we read again the CTRLA then the DSCR till two
  582. * consecutive read values from DSCR are equal or till the maximum trials is
  583. * reach. This algorithm is very unlikely not to find a stable value for DSCR.
  584. *
  585. * Returns: %0 on success, -errno otherwise.
  586. */
  587. static int atc_get_llis_residue(struct at_dma_chan *atchan,
  588. struct at_desc *desc, u32 *residue)
  589. {
  590. u32 len, ctrla, dscr;
  591. unsigned int i;
  592. len = desc->total_len;
  593. dscr = channel_readl(atchan, DSCR);
  594. rmb(); /* ensure DSCR is read before CTRLA */
  595. ctrla = channel_readl(atchan, CTRLA);
  596. for (i = 0; i < ATC_MAX_DSCR_TRIALS; ++i) {
  597. u32 new_dscr;
  598. rmb(); /* ensure DSCR is read after CTRLA */
  599. new_dscr = channel_readl(atchan, DSCR);
  600. /*
  601. * If the DSCR register value has not changed inside the DMA
  602. * controller since the previous read, we assume that both the
  603. * dscr and ctrla values refers to the very same descriptor.
  604. */
  605. if (likely(new_dscr == dscr))
  606. break;
  607. /*
  608. * DSCR has changed inside the DMA controller, so the previously
  609. * read value of CTRLA may refer to an already processed
  610. * descriptor hence could be outdated. We need to update ctrla
  611. * to match the current descriptor.
  612. */
  613. dscr = new_dscr;
  614. rmb(); /* ensure DSCR is read before CTRLA */
  615. ctrla = channel_readl(atchan, CTRLA);
  616. }
  617. if (unlikely(i == ATC_MAX_DSCR_TRIALS))
  618. return -ETIMEDOUT;
  619. /* For the first descriptor we can be more accurate. */
  620. if (desc->sg[0].lli->dscr == dscr) {
  621. *residue = atc_calc_bytes_left(len, ctrla);
  622. return 0;
  623. }
  624. len -= desc->sg[0].len;
  625. for (i = 1; i < desc->sglen; i++) {
  626. if (desc->sg[i].lli && desc->sg[i].lli->dscr == dscr)
  627. break;
  628. len -= desc->sg[i].len;
  629. }
  630. /*
  631. * For the current LLI in the chain we can calculate the remaining bytes
  632. * using the channel's CTRLA register.
  633. */
  634. *residue = atc_calc_bytes_left(len, ctrla);
  635. return 0;
  636. }
  637. /**
  638. * atc_get_residue - get the number of bytes residue for a cookie.
  639. * The residue is passed by address and updated on success.
  640. * @chan: DMA channel
  641. * @cookie: transaction identifier to check status of
  642. * @residue: residue to be updated.
  643. *
  644. * Return: %0 on success, -errno otherwise.
  645. */
  646. static int atc_get_residue(struct dma_chan *chan, dma_cookie_t cookie,
  647. u32 *residue)
  648. {
  649. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  650. struct virt_dma_desc *vd;
  651. struct at_desc *desc = NULL;
  652. u32 len, ctrla;
  653. vd = vchan_find_desc(&atchan->vc, cookie);
  654. if (vd)
  655. desc = to_atdma_desc(&vd->tx);
  656. else if (atchan->desc && atchan->desc->vd.tx.cookie == cookie)
  657. desc = atchan->desc;
  658. if (!desc)
  659. return -EINVAL;
  660. if (desc->sg[0].lli->dscr)
  661. /* hardware linked list transfer */
  662. return atc_get_llis_residue(atchan, desc, residue);
  663. /* single transfer */
  664. len = desc->total_len;
  665. ctrla = channel_readl(atchan, CTRLA);
  666. *residue = atc_calc_bytes_left(len, ctrla);
  667. return 0;
  668. }
  669. /**
  670. * atc_handle_error - handle errors reported by DMA controller
  671. * @atchan: channel where error occurs.
  672. * @i: channel index
  673. */
  674. static void atc_handle_error(struct at_dma_chan *atchan, unsigned int i)
  675. {
  676. struct at_desc *desc = atchan->desc;
  677. /* Disable channel on AHB error */
  678. dma_writel(atchan->atdma, CHDR, AT_DMA_RES(i) | atchan->mask);
  679. /*
  680. * KERN_CRITICAL may seem harsh, but since this only happens
  681. * when someone submits a bad physical address in a
  682. * descriptor, we should consider ourselves lucky that the
  683. * controller flagged an error instead of scribbling over
  684. * random memory locations.
  685. */
  686. dev_crit(chan2dev(&atchan->vc.chan), "Bad descriptor submitted for DMA!\n");
  687. dev_crit(chan2dev(&atchan->vc.chan), "cookie: %d\n",
  688. desc->vd.tx.cookie);
  689. for (i = 0; i < desc->sglen; i++)
  690. atc_dump_lli(atchan, desc->sg[i].lli);
  691. }
  692. static void atdma_handle_chan_done(struct at_dma_chan *atchan, u32 pending,
  693. unsigned int i)
  694. {
  695. struct at_desc *desc;
  696. spin_lock(&atchan->vc.lock);
  697. desc = atchan->desc;
  698. if (desc) {
  699. if (pending & AT_DMA_ERR(i)) {
  700. atc_handle_error(atchan, i);
  701. /* Pretend the descriptor completed successfully */
  702. }
  703. if (atc_chan_is_cyclic(atchan)) {
  704. vchan_cyclic_callback(&desc->vd);
  705. } else {
  706. vchan_cookie_complete(&desc->vd);
  707. atchan->desc = NULL;
  708. if (!(atc_chan_is_enabled(atchan)))
  709. atc_dostart(atchan);
  710. }
  711. }
  712. spin_unlock(&atchan->vc.lock);
  713. }
  714. static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
  715. {
  716. struct at_dma *atdma = dev_id;
  717. struct at_dma_chan *atchan;
  718. int i;
  719. u32 status, pending, imr;
  720. int ret = IRQ_NONE;
  721. do {
  722. imr = dma_readl(atdma, EBCIMR);
  723. status = dma_readl(atdma, EBCISR);
  724. pending = status & imr;
  725. if (!pending)
  726. break;
  727. dev_vdbg(atdma->dma_device.dev,
  728. "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
  729. status, imr, pending);
  730. for (i = 0; i < atdma->dma_device.chancnt; i++) {
  731. atchan = &atdma->chan[i];
  732. if (!(pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))))
  733. continue;
  734. atdma_handle_chan_done(atchan, pending, i);
  735. ret = IRQ_HANDLED;
  736. }
  737. } while (pending);
  738. return ret;
  739. }
  740. /*-- DMA Engine API --------------------------------------------------*/
  741. /**
  742. * atc_prep_dma_interleaved - prepare memory to memory interleaved operation
  743. * @chan: the channel to prepare operation on
  744. * @xt: Interleaved transfer template
  745. * @flags: tx descriptor status flags
  746. */
  747. static struct dma_async_tx_descriptor *
  748. atc_prep_dma_interleaved(struct dma_chan *chan,
  749. struct dma_interleaved_template *xt,
  750. unsigned long flags)
  751. {
  752. struct at_dma *atdma = to_at_dma(chan->device);
  753. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  754. struct data_chunk *first;
  755. struct atdma_sg *atdma_sg;
  756. struct at_desc *desc;
  757. struct at_lli *lli;
  758. size_t xfer_count;
  759. unsigned int dwidth;
  760. u32 ctrla;
  761. u32 ctrlb;
  762. size_t len = 0;
  763. int i;
  764. if (unlikely(!xt || xt->numf != 1 || !xt->frame_size))
  765. return NULL;
  766. first = xt->sgl;
  767. dev_info(chan2dev(chan),
  768. "%s: src=%pad, dest=%pad, numf=%d, frame_size=%d, flags=0x%lx\n",
  769. __func__, &xt->src_start, &xt->dst_start, xt->numf,
  770. xt->frame_size, flags);
  771. /*
  772. * The controller can only "skip" X bytes every Y bytes, so we
  773. * need to make sure we are given a template that fit that
  774. * description, ie a template with chunks that always have the
  775. * same size, with the same ICGs.
  776. */
  777. for (i = 0; i < xt->frame_size; i++) {
  778. struct data_chunk *chunk = xt->sgl + i;
  779. if ((chunk->size != xt->sgl->size) ||
  780. (dmaengine_get_dst_icg(xt, chunk) != dmaengine_get_dst_icg(xt, first)) ||
  781. (dmaengine_get_src_icg(xt, chunk) != dmaengine_get_src_icg(xt, first))) {
  782. dev_err(chan2dev(chan),
  783. "%s: the controller can transfer only identical chunks\n",
  784. __func__);
  785. return NULL;
  786. }
  787. len += chunk->size;
  788. }
  789. dwidth = atc_get_xfer_width(xt->src_start, xt->dst_start, len);
  790. xfer_count = len >> dwidth;
  791. if (xfer_count > ATC_BTSIZE_MAX) {
  792. dev_err(chan2dev(chan), "%s: buffer is too big\n", __func__);
  793. return NULL;
  794. }
  795. ctrla = FIELD_PREP(ATC_SRC_WIDTH, dwidth) |
  796. FIELD_PREP(ATC_DST_WIDTH, dwidth);
  797. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN |
  798. FIELD_PREP(ATC_SRC_ADDR_MODE, ATC_SRC_ADDR_MODE_INCR) |
  799. FIELD_PREP(ATC_DST_ADDR_MODE, ATC_DST_ADDR_MODE_INCR) |
  800. ATC_SRC_PIP | ATC_DST_PIP |
  801. FIELD_PREP(ATC_FC, ATC_FC_MEM2MEM);
  802. desc = kzalloc(struct_size(desc, sg, 1), GFP_ATOMIC);
  803. if (!desc)
  804. return NULL;
  805. desc->sglen = 1;
  806. atdma_sg = desc->sg;
  807. atdma_sg->lli = dma_pool_alloc(atdma->lli_pool, GFP_NOWAIT,
  808. &atdma_sg->lli_phys);
  809. if (!atdma_sg->lli) {
  810. kfree(desc);
  811. return NULL;
  812. }
  813. lli = atdma_sg->lli;
  814. lli->saddr = xt->src_start;
  815. lli->daddr = xt->dst_start;
  816. lli->ctrla = ctrla | xfer_count;
  817. lli->ctrlb = ctrlb;
  818. desc->boundary = first->size >> dwidth;
  819. desc->dst_hole = (dmaengine_get_dst_icg(xt, first) >> dwidth) + 1;
  820. desc->src_hole = (dmaengine_get_src_icg(xt, first) >> dwidth) + 1;
  821. atdma_sg->len = len;
  822. desc->total_len = len;
  823. set_lli_eol(desc, 0);
  824. return vchan_tx_prep(&atchan->vc, &desc->vd, flags);
  825. }
  826. /**
  827. * atc_prep_dma_memcpy - prepare a memcpy operation
  828. * @chan: the channel to prepare operation on
  829. * @dest: operation virtual destination address
  830. * @src: operation virtual source address
  831. * @len: operation length
  832. * @flags: tx descriptor status flags
  833. */
  834. static struct dma_async_tx_descriptor *
  835. atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  836. size_t len, unsigned long flags)
  837. {
  838. struct at_dma *atdma = to_at_dma(chan->device);
  839. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  840. struct at_desc *desc = NULL;
  841. size_t xfer_count;
  842. size_t offset;
  843. size_t sg_len;
  844. unsigned int src_width;
  845. unsigned int dst_width;
  846. unsigned int i;
  847. u32 ctrla;
  848. u32 ctrlb;
  849. dev_dbg(chan2dev(chan), "prep_dma_memcpy: d%pad s%pad l0x%zx f0x%lx\n",
  850. &dest, &src, len, flags);
  851. if (unlikely(!len)) {
  852. dev_err(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  853. return NULL;
  854. }
  855. sg_len = DIV_ROUND_UP(len, ATC_BTSIZE_MAX);
  856. desc = kzalloc(struct_size(desc, sg, sg_len), GFP_ATOMIC);
  857. if (!desc)
  858. return NULL;
  859. desc->sglen = sg_len;
  860. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN |
  861. FIELD_PREP(ATC_SRC_ADDR_MODE, ATC_SRC_ADDR_MODE_INCR) |
  862. FIELD_PREP(ATC_DST_ADDR_MODE, ATC_DST_ADDR_MODE_INCR) |
  863. FIELD_PREP(ATC_FC, ATC_FC_MEM2MEM);
  864. /*
  865. * We can be a lot more clever here, but this should take care
  866. * of the most common optimization.
  867. */
  868. src_width = dst_width = atc_get_xfer_width(src, dest, len);
  869. ctrla = FIELD_PREP(ATC_SRC_WIDTH, src_width) |
  870. FIELD_PREP(ATC_DST_WIDTH, dst_width);
  871. for (offset = 0, i = 0; offset < len;
  872. offset += xfer_count << src_width, i++) {
  873. struct atdma_sg *atdma_sg = &desc->sg[i];
  874. struct at_lli *lli;
  875. atdma_sg->lli = dma_pool_alloc(atdma->lli_pool, GFP_NOWAIT,
  876. &atdma_sg->lli_phys);
  877. if (!atdma_sg->lli)
  878. goto err_desc_get;
  879. lli = atdma_sg->lli;
  880. xfer_count = min_t(size_t, (len - offset) >> src_width,
  881. ATC_BTSIZE_MAX);
  882. lli->saddr = src + offset;
  883. lli->daddr = dest + offset;
  884. lli->ctrla = ctrla | xfer_count;
  885. lli->ctrlb = ctrlb;
  886. desc->sg[i].len = xfer_count << src_width;
  887. atdma_lli_chain(desc, i);
  888. }
  889. desc->total_len = len;
  890. /* set end-of-link to the last link descriptor of list*/
  891. set_lli_eol(desc, i - 1);
  892. return vchan_tx_prep(&atchan->vc, &desc->vd, flags);
  893. err_desc_get:
  894. atdma_desc_free(&desc->vd);
  895. return NULL;
  896. }
  897. static int atdma_create_memset_lli(struct dma_chan *chan,
  898. struct atdma_sg *atdma_sg,
  899. dma_addr_t psrc, dma_addr_t pdst, size_t len)
  900. {
  901. struct at_dma *atdma = to_at_dma(chan->device);
  902. struct at_lli *lli;
  903. size_t xfer_count;
  904. u32 ctrla = FIELD_PREP(ATC_SRC_WIDTH, 2) | FIELD_PREP(ATC_DST_WIDTH, 2);
  905. u32 ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN |
  906. FIELD_PREP(ATC_SRC_ADDR_MODE, ATC_SRC_ADDR_MODE_FIXED) |
  907. FIELD_PREP(ATC_DST_ADDR_MODE, ATC_DST_ADDR_MODE_INCR) |
  908. FIELD_PREP(ATC_FC, ATC_FC_MEM2MEM);
  909. xfer_count = len >> 2;
  910. if (xfer_count > ATC_BTSIZE_MAX) {
  911. dev_err(chan2dev(chan), "%s: buffer is too big\n", __func__);
  912. return -EINVAL;
  913. }
  914. atdma_sg->lli = dma_pool_alloc(atdma->lli_pool, GFP_NOWAIT,
  915. &atdma_sg->lli_phys);
  916. if (!atdma_sg->lli)
  917. return -ENOMEM;
  918. lli = atdma_sg->lli;
  919. lli->saddr = psrc;
  920. lli->daddr = pdst;
  921. lli->ctrla = ctrla | xfer_count;
  922. lli->ctrlb = ctrlb;
  923. atdma_sg->len = len;
  924. return 0;
  925. }
  926. /**
  927. * atc_prep_dma_memset - prepare a memcpy operation
  928. * @chan: the channel to prepare operation on
  929. * @dest: operation virtual destination address
  930. * @value: value to set memory buffer to
  931. * @len: operation length
  932. * @flags: tx descriptor status flags
  933. */
  934. static struct dma_async_tx_descriptor *
  935. atc_prep_dma_memset(struct dma_chan *chan, dma_addr_t dest, int value,
  936. size_t len, unsigned long flags)
  937. {
  938. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  939. struct at_dma *atdma = to_at_dma(chan->device);
  940. struct at_desc *desc;
  941. void __iomem *vaddr;
  942. dma_addr_t paddr;
  943. char fill_pattern;
  944. int ret;
  945. dev_vdbg(chan2dev(chan), "%s: d%pad v0x%x l0x%zx f0x%lx\n", __func__,
  946. &dest, value, len, flags);
  947. if (unlikely(!len)) {
  948. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  949. return NULL;
  950. }
  951. if (!is_dma_fill_aligned(chan->device, dest, 0, len)) {
  952. dev_dbg(chan2dev(chan), "%s: buffer is not aligned\n",
  953. __func__);
  954. return NULL;
  955. }
  956. vaddr = dma_pool_alloc(atdma->memset_pool, GFP_NOWAIT, &paddr);
  957. if (!vaddr) {
  958. dev_err(chan2dev(chan), "%s: couldn't allocate buffer\n",
  959. __func__);
  960. return NULL;
  961. }
  962. /* Only the first byte of value is to be used according to dmaengine */
  963. fill_pattern = (char)value;
  964. *(u32*)vaddr = (fill_pattern << 24) |
  965. (fill_pattern << 16) |
  966. (fill_pattern << 8) |
  967. fill_pattern;
  968. desc = kzalloc(struct_size(desc, sg, 1), GFP_ATOMIC);
  969. if (!desc)
  970. goto err_free_buffer;
  971. desc->sglen = 1;
  972. ret = atdma_create_memset_lli(chan, desc->sg, paddr, dest, len);
  973. if (ret)
  974. goto err_free_desc;
  975. desc->memset_paddr = paddr;
  976. desc->memset_vaddr = vaddr;
  977. desc->memset_buffer = true;
  978. desc->total_len = len;
  979. /* set end-of-link on the descriptor */
  980. set_lli_eol(desc, 0);
  981. return vchan_tx_prep(&atchan->vc, &desc->vd, flags);
  982. err_free_desc:
  983. kfree(desc);
  984. err_free_buffer:
  985. dma_pool_free(atdma->memset_pool, vaddr, paddr);
  986. return NULL;
  987. }
  988. static struct dma_async_tx_descriptor *
  989. atc_prep_dma_memset_sg(struct dma_chan *chan,
  990. struct scatterlist *sgl,
  991. unsigned int sg_len, int value,
  992. unsigned long flags)
  993. {
  994. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  995. struct at_dma *atdma = to_at_dma(chan->device);
  996. struct at_desc *desc;
  997. struct scatterlist *sg;
  998. void __iomem *vaddr;
  999. dma_addr_t paddr;
  1000. size_t total_len = 0;
  1001. int i;
  1002. int ret;
  1003. dev_vdbg(chan2dev(chan), "%s: v0x%x l0x%zx f0x%lx\n", __func__,
  1004. value, sg_len, flags);
  1005. if (unlikely(!sgl || !sg_len)) {
  1006. dev_dbg(chan2dev(chan), "%s: scatterlist is empty!\n",
  1007. __func__);
  1008. return NULL;
  1009. }
  1010. vaddr = dma_pool_alloc(atdma->memset_pool, GFP_NOWAIT, &paddr);
  1011. if (!vaddr) {
  1012. dev_err(chan2dev(chan), "%s: couldn't allocate buffer\n",
  1013. __func__);
  1014. return NULL;
  1015. }
  1016. *(u32*)vaddr = value;
  1017. desc = kzalloc(struct_size(desc, sg, sg_len), GFP_ATOMIC);
  1018. if (!desc)
  1019. goto err_free_dma_buf;
  1020. desc->sglen = sg_len;
  1021. for_each_sg(sgl, sg, sg_len, i) {
  1022. dma_addr_t dest = sg_dma_address(sg);
  1023. size_t len = sg_dma_len(sg);
  1024. dev_vdbg(chan2dev(chan), "%s: d%pad, l0x%zx\n",
  1025. __func__, &dest, len);
  1026. if (!is_dma_fill_aligned(chan->device, dest, 0, len)) {
  1027. dev_err(chan2dev(chan), "%s: buffer is not aligned\n",
  1028. __func__);
  1029. goto err_free_desc;
  1030. }
  1031. ret = atdma_create_memset_lli(chan, &desc->sg[i], paddr, dest,
  1032. len);
  1033. if (ret)
  1034. goto err_free_desc;
  1035. atdma_lli_chain(desc, i);
  1036. total_len += len;
  1037. }
  1038. desc->memset_paddr = paddr;
  1039. desc->memset_vaddr = vaddr;
  1040. desc->memset_buffer = true;
  1041. desc->total_len = total_len;
  1042. /* set end-of-link on the descriptor */
  1043. set_lli_eol(desc, i - 1);
  1044. return vchan_tx_prep(&atchan->vc, &desc->vd, flags);
  1045. err_free_desc:
  1046. atdma_desc_free(&desc->vd);
  1047. err_free_dma_buf:
  1048. dma_pool_free(atdma->memset_pool, vaddr, paddr);
  1049. return NULL;
  1050. }
  1051. /**
  1052. * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  1053. * @chan: DMA channel
  1054. * @sgl: scatterlist to transfer to/from
  1055. * @sg_len: number of entries in @scatterlist
  1056. * @direction: DMA direction
  1057. * @flags: tx descriptor status flags
  1058. * @context: transaction context (ignored)
  1059. */
  1060. static struct dma_async_tx_descriptor *
  1061. atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  1062. unsigned int sg_len, enum dma_transfer_direction direction,
  1063. unsigned long flags, void *context)
  1064. {
  1065. struct at_dma *atdma = to_at_dma(chan->device);
  1066. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1067. struct at_dma_slave *atslave = chan->private;
  1068. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  1069. struct at_desc *desc;
  1070. u32 ctrla;
  1071. u32 ctrlb;
  1072. dma_addr_t reg;
  1073. unsigned int reg_width;
  1074. unsigned int mem_width;
  1075. unsigned int i;
  1076. struct scatterlist *sg;
  1077. size_t total_len = 0;
  1078. dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
  1079. sg_len,
  1080. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  1081. flags);
  1082. if (unlikely(!atslave || !sg_len)) {
  1083. dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
  1084. return NULL;
  1085. }
  1086. desc = kzalloc(struct_size(desc, sg, sg_len), GFP_ATOMIC);
  1087. if (!desc)
  1088. return NULL;
  1089. desc->sglen = sg_len;
  1090. ctrla = FIELD_PREP(ATC_SCSIZE, sconfig->src_maxburst) |
  1091. FIELD_PREP(ATC_DCSIZE, sconfig->dst_maxburst);
  1092. ctrlb = ATC_IEN;
  1093. switch (direction) {
  1094. case DMA_MEM_TO_DEV:
  1095. reg_width = convert_buswidth(sconfig->dst_addr_width);
  1096. ctrla |= FIELD_PREP(ATC_DST_WIDTH, reg_width);
  1097. ctrlb |= FIELD_PREP(ATC_DST_ADDR_MODE,
  1098. ATC_DST_ADDR_MODE_FIXED) |
  1099. FIELD_PREP(ATC_SRC_ADDR_MODE, ATC_SRC_ADDR_MODE_INCR) |
  1100. FIELD_PREP(ATC_FC, ATC_FC_MEM2PER) |
  1101. FIELD_PREP(ATC_SIF, atchan->mem_if) |
  1102. FIELD_PREP(ATC_DIF, atchan->per_if);
  1103. reg = sconfig->dst_addr;
  1104. for_each_sg(sgl, sg, sg_len, i) {
  1105. struct atdma_sg *atdma_sg = &desc->sg[i];
  1106. struct at_lli *lli;
  1107. u32 len;
  1108. u32 mem;
  1109. atdma_sg->lli = dma_pool_alloc(atdma->lli_pool,
  1110. GFP_NOWAIT,
  1111. &atdma_sg->lli_phys);
  1112. if (!atdma_sg->lli)
  1113. goto err_desc_get;
  1114. lli = atdma_sg->lli;
  1115. mem = sg_dma_address(sg);
  1116. len = sg_dma_len(sg);
  1117. if (unlikely(!len)) {
  1118. dev_dbg(chan2dev(chan),
  1119. "prep_slave_sg: sg(%d) data length is zero\n", i);
  1120. goto err;
  1121. }
  1122. mem_width = 2;
  1123. if (unlikely(mem & 3 || len & 3))
  1124. mem_width = 0;
  1125. lli->saddr = mem;
  1126. lli->daddr = reg;
  1127. lli->ctrla = ctrla |
  1128. FIELD_PREP(ATC_SRC_WIDTH, mem_width) |
  1129. len >> mem_width;
  1130. lli->ctrlb = ctrlb;
  1131. atdma_sg->len = len;
  1132. total_len += len;
  1133. desc->sg[i].len = len;
  1134. atdma_lli_chain(desc, i);
  1135. }
  1136. break;
  1137. case DMA_DEV_TO_MEM:
  1138. reg_width = convert_buswidth(sconfig->src_addr_width);
  1139. ctrla |= FIELD_PREP(ATC_SRC_WIDTH, reg_width);
  1140. ctrlb |= FIELD_PREP(ATC_DST_ADDR_MODE, ATC_DST_ADDR_MODE_INCR) |
  1141. FIELD_PREP(ATC_SRC_ADDR_MODE,
  1142. ATC_SRC_ADDR_MODE_FIXED) |
  1143. FIELD_PREP(ATC_FC, ATC_FC_PER2MEM) |
  1144. FIELD_PREP(ATC_SIF, atchan->per_if) |
  1145. FIELD_PREP(ATC_DIF, atchan->mem_if);
  1146. reg = sconfig->src_addr;
  1147. for_each_sg(sgl, sg, sg_len, i) {
  1148. struct atdma_sg *atdma_sg = &desc->sg[i];
  1149. struct at_lli *lli;
  1150. u32 len;
  1151. u32 mem;
  1152. atdma_sg->lli = dma_pool_alloc(atdma->lli_pool,
  1153. GFP_NOWAIT,
  1154. &atdma_sg->lli_phys);
  1155. if (!atdma_sg->lli)
  1156. goto err_desc_get;
  1157. lli = atdma_sg->lli;
  1158. mem = sg_dma_address(sg);
  1159. len = sg_dma_len(sg);
  1160. if (unlikely(!len)) {
  1161. dev_dbg(chan2dev(chan),
  1162. "prep_slave_sg: sg(%d) data length is zero\n", i);
  1163. goto err;
  1164. }
  1165. mem_width = 2;
  1166. if (unlikely(mem & 3 || len & 3))
  1167. mem_width = 0;
  1168. lli->saddr = reg;
  1169. lli->daddr = mem;
  1170. lli->ctrla = ctrla |
  1171. FIELD_PREP(ATC_DST_WIDTH, mem_width) |
  1172. len >> reg_width;
  1173. lli->ctrlb = ctrlb;
  1174. desc->sg[i].len = len;
  1175. total_len += len;
  1176. atdma_lli_chain(desc, i);
  1177. }
  1178. break;
  1179. default:
  1180. return NULL;
  1181. }
  1182. /* set end-of-link to the last link descriptor of list*/
  1183. set_lli_eol(desc, i - 1);
  1184. desc->total_len = total_len;
  1185. return vchan_tx_prep(&atchan->vc, &desc->vd, flags);
  1186. err_desc_get:
  1187. dev_err(chan2dev(chan), "not enough descriptors available\n");
  1188. err:
  1189. atdma_desc_free(&desc->vd);
  1190. return NULL;
  1191. }
  1192. /*
  1193. * atc_dma_cyclic_check_values
  1194. * Check for too big/unaligned periods and unaligned DMA buffer
  1195. */
  1196. static int
  1197. atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
  1198. size_t period_len)
  1199. {
  1200. if (period_len > (ATC_BTSIZE_MAX << reg_width))
  1201. goto err_out;
  1202. if (unlikely(period_len & ((1 << reg_width) - 1)))
  1203. goto err_out;
  1204. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1205. goto err_out;
  1206. return 0;
  1207. err_out:
  1208. return -EINVAL;
  1209. }
  1210. /*
  1211. * atc_dma_cyclic_fill_desc - Fill one period descriptor
  1212. */
  1213. static int
  1214. atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
  1215. unsigned int i, dma_addr_t buf_addr,
  1216. unsigned int reg_width, size_t period_len,
  1217. enum dma_transfer_direction direction)
  1218. {
  1219. struct at_dma *atdma = to_at_dma(chan->device);
  1220. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1221. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  1222. struct atdma_sg *atdma_sg = &desc->sg[i];
  1223. struct at_lli *lli;
  1224. atdma_sg->lli = dma_pool_alloc(atdma->lli_pool, GFP_ATOMIC,
  1225. &atdma_sg->lli_phys);
  1226. if (!atdma_sg->lli)
  1227. return -ENOMEM;
  1228. lli = atdma_sg->lli;
  1229. switch (direction) {
  1230. case DMA_MEM_TO_DEV:
  1231. lli->saddr = buf_addr + (period_len * i);
  1232. lli->daddr = sconfig->dst_addr;
  1233. lli->ctrlb = FIELD_PREP(ATC_DST_ADDR_MODE,
  1234. ATC_DST_ADDR_MODE_FIXED) |
  1235. FIELD_PREP(ATC_SRC_ADDR_MODE,
  1236. ATC_SRC_ADDR_MODE_INCR) |
  1237. FIELD_PREP(ATC_FC, ATC_FC_MEM2PER) |
  1238. FIELD_PREP(ATC_SIF, atchan->mem_if) |
  1239. FIELD_PREP(ATC_DIF, atchan->per_if);
  1240. break;
  1241. case DMA_DEV_TO_MEM:
  1242. lli->saddr = sconfig->src_addr;
  1243. lli->daddr = buf_addr + (period_len * i);
  1244. lli->ctrlb = FIELD_PREP(ATC_DST_ADDR_MODE,
  1245. ATC_DST_ADDR_MODE_INCR) |
  1246. FIELD_PREP(ATC_SRC_ADDR_MODE,
  1247. ATC_SRC_ADDR_MODE_FIXED) |
  1248. FIELD_PREP(ATC_FC, ATC_FC_PER2MEM) |
  1249. FIELD_PREP(ATC_SIF, atchan->per_if) |
  1250. FIELD_PREP(ATC_DIF, atchan->mem_if);
  1251. break;
  1252. default:
  1253. return -EINVAL;
  1254. }
  1255. lli->ctrla = FIELD_PREP(ATC_SCSIZE, sconfig->src_maxburst) |
  1256. FIELD_PREP(ATC_DCSIZE, sconfig->dst_maxburst) |
  1257. FIELD_PREP(ATC_DST_WIDTH, reg_width) |
  1258. FIELD_PREP(ATC_SRC_WIDTH, reg_width) |
  1259. period_len >> reg_width;
  1260. desc->sg[i].len = period_len;
  1261. return 0;
  1262. }
  1263. /**
  1264. * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
  1265. * @chan: the DMA channel to prepare
  1266. * @buf_addr: physical DMA address where the buffer starts
  1267. * @buf_len: total number of bytes for the entire buffer
  1268. * @period_len: number of bytes for each period
  1269. * @direction: transfer direction, to or from device
  1270. * @flags: tx descriptor status flags
  1271. */
  1272. static struct dma_async_tx_descriptor *
  1273. atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  1274. size_t period_len, enum dma_transfer_direction direction,
  1275. unsigned long flags)
  1276. {
  1277. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1278. struct at_dma_slave *atslave = chan->private;
  1279. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  1280. struct at_desc *desc;
  1281. unsigned long was_cyclic;
  1282. unsigned int reg_width;
  1283. unsigned int periods = buf_len / period_len;
  1284. unsigned int i;
  1285. dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@%pad - %d (%d/%d)\n",
  1286. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  1287. &buf_addr,
  1288. periods, buf_len, period_len);
  1289. if (unlikely(!atslave || !buf_len || !period_len)) {
  1290. dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
  1291. return NULL;
  1292. }
  1293. was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
  1294. if (was_cyclic) {
  1295. dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
  1296. return NULL;
  1297. }
  1298. if (unlikely(!is_slave_direction(direction)))
  1299. goto err_out;
  1300. if (direction == DMA_MEM_TO_DEV)
  1301. reg_width = convert_buswidth(sconfig->dst_addr_width);
  1302. else
  1303. reg_width = convert_buswidth(sconfig->src_addr_width);
  1304. /* Check for too big/unaligned periods and unaligned DMA buffer */
  1305. if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
  1306. goto err_out;
  1307. desc = kzalloc(struct_size(desc, sg, periods), GFP_ATOMIC);
  1308. if (!desc)
  1309. goto err_out;
  1310. desc->sglen = periods;
  1311. /* build cyclic linked list */
  1312. for (i = 0; i < periods; i++) {
  1313. if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
  1314. reg_width, period_len, direction))
  1315. goto err_fill_desc;
  1316. atdma_lli_chain(desc, i);
  1317. }
  1318. desc->total_len = buf_len;
  1319. /* lets make a cyclic list */
  1320. desc->sg[i - 1].lli->dscr = desc->sg[0].lli_phys;
  1321. return vchan_tx_prep(&atchan->vc, &desc->vd, flags);
  1322. err_fill_desc:
  1323. atdma_desc_free(&desc->vd);
  1324. err_out:
  1325. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  1326. return NULL;
  1327. }
  1328. static int atc_config(struct dma_chan *chan,
  1329. struct dma_slave_config *sconfig)
  1330. {
  1331. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1332. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1333. /* Check if it is chan is configured for slave transfers */
  1334. if (!chan->private)
  1335. return -EINVAL;
  1336. memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
  1337. convert_burst(&atchan->dma_sconfig.src_maxburst);
  1338. convert_burst(&atchan->dma_sconfig.dst_maxburst);
  1339. return 0;
  1340. }
  1341. static int atc_pause(struct dma_chan *chan)
  1342. {
  1343. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1344. struct at_dma *atdma = to_at_dma(chan->device);
  1345. int chan_id = atchan->vc.chan.chan_id;
  1346. unsigned long flags;
  1347. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1348. spin_lock_irqsave(&atchan->vc.lock, flags);
  1349. dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
  1350. set_bit(ATC_IS_PAUSED, &atchan->status);
  1351. spin_unlock_irqrestore(&atchan->vc.lock, flags);
  1352. return 0;
  1353. }
  1354. static int atc_resume(struct dma_chan *chan)
  1355. {
  1356. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1357. struct at_dma *atdma = to_at_dma(chan->device);
  1358. int chan_id = atchan->vc.chan.chan_id;
  1359. unsigned long flags;
  1360. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1361. if (!atc_chan_is_paused(atchan))
  1362. return 0;
  1363. spin_lock_irqsave(&atchan->vc.lock, flags);
  1364. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
  1365. clear_bit(ATC_IS_PAUSED, &atchan->status);
  1366. spin_unlock_irqrestore(&atchan->vc.lock, flags);
  1367. return 0;
  1368. }
  1369. static int atc_terminate_all(struct dma_chan *chan)
  1370. {
  1371. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1372. struct at_dma *atdma = to_at_dma(chan->device);
  1373. int chan_id = atchan->vc.chan.chan_id;
  1374. unsigned long flags;
  1375. LIST_HEAD(list);
  1376. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  1377. /*
  1378. * This is only called when something went wrong elsewhere, so
  1379. * we don't really care about the data. Just disable the
  1380. * channel. We still have to poll the channel enable bit due
  1381. * to AHB/HSB limitations.
  1382. */
  1383. spin_lock_irqsave(&atchan->vc.lock, flags);
  1384. /* disabling channel: must also remove suspend state */
  1385. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
  1386. /* confirm that this channel is disabled */
  1387. while (dma_readl(atdma, CHSR) & atchan->mask)
  1388. cpu_relax();
  1389. if (atchan->desc) {
  1390. vchan_terminate_vdesc(&atchan->desc->vd);
  1391. atchan->desc = NULL;
  1392. }
  1393. vchan_get_all_descriptors(&atchan->vc, &list);
  1394. clear_bit(ATC_IS_PAUSED, &atchan->status);
  1395. /* if channel dedicated to cyclic operations, free it */
  1396. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  1397. spin_unlock_irqrestore(&atchan->vc.lock, flags);
  1398. vchan_dma_desc_free_list(&atchan->vc, &list);
  1399. return 0;
  1400. }
  1401. /**
  1402. * atc_tx_status - poll for transaction completion
  1403. * @chan: DMA channel
  1404. * @cookie: transaction identifier to check status of
  1405. * @txstate: if not %NULL updated with transaction state
  1406. *
  1407. * If @txstate is passed in, upon return it reflect the driver
  1408. * internal state and can be used with dma_async_is_complete() to check
  1409. * the status of multiple cookies without re-checking hardware state.
  1410. */
  1411. static enum dma_status
  1412. atc_tx_status(struct dma_chan *chan,
  1413. dma_cookie_t cookie,
  1414. struct dma_tx_state *txstate)
  1415. {
  1416. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1417. unsigned long flags;
  1418. enum dma_status dma_status;
  1419. u32 residue;
  1420. int ret;
  1421. dma_status = dma_cookie_status(chan, cookie, txstate);
  1422. if (dma_status == DMA_COMPLETE || !txstate)
  1423. return dma_status;
  1424. spin_lock_irqsave(&atchan->vc.lock, flags);
  1425. /* Get number of bytes left in the active transactions */
  1426. ret = atc_get_residue(chan, cookie, &residue);
  1427. spin_unlock_irqrestore(&atchan->vc.lock, flags);
  1428. if (unlikely(ret < 0)) {
  1429. dev_vdbg(chan2dev(chan), "get residual bytes error\n");
  1430. return DMA_ERROR;
  1431. } else {
  1432. dma_set_residue(txstate, residue);
  1433. }
  1434. dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %u\n",
  1435. dma_status, cookie, residue);
  1436. return dma_status;
  1437. }
  1438. static void atc_issue_pending(struct dma_chan *chan)
  1439. {
  1440. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1441. unsigned long flags;
  1442. spin_lock_irqsave(&atchan->vc.lock, flags);
  1443. if (vchan_issue_pending(&atchan->vc) && !atchan->desc) {
  1444. if (!(atc_chan_is_enabled(atchan)))
  1445. atc_dostart(atchan);
  1446. }
  1447. spin_unlock_irqrestore(&atchan->vc.lock, flags);
  1448. }
  1449. /**
  1450. * atc_alloc_chan_resources - allocate resources for DMA channel
  1451. * @chan: allocate descriptor resources for this channel
  1452. *
  1453. * Return: the number of allocated descriptors
  1454. */
  1455. static int atc_alloc_chan_resources(struct dma_chan *chan)
  1456. {
  1457. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1458. struct at_dma *atdma = to_at_dma(chan->device);
  1459. struct at_dma_slave *atslave;
  1460. u32 cfg;
  1461. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  1462. /* ASSERT: channel is idle */
  1463. if (atc_chan_is_enabled(atchan)) {
  1464. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  1465. return -EIO;
  1466. }
  1467. cfg = ATC_DEFAULT_CFG;
  1468. atslave = chan->private;
  1469. if (atslave) {
  1470. /*
  1471. * We need controller-specific data to set up slave
  1472. * transfers.
  1473. */
  1474. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_device.dev);
  1475. /* if cfg configuration specified take it instead of default */
  1476. if (atslave->cfg)
  1477. cfg = atslave->cfg;
  1478. }
  1479. /* channel parameters */
  1480. channel_writel(atchan, CFG, cfg);
  1481. return 0;
  1482. }
  1483. /**
  1484. * atc_free_chan_resources - free all channel resources
  1485. * @chan: DMA channel
  1486. */
  1487. static void atc_free_chan_resources(struct dma_chan *chan)
  1488. {
  1489. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1490. BUG_ON(atc_chan_is_enabled(atchan));
  1491. vchan_free_chan_resources(to_virt_chan(chan));
  1492. atchan->status = 0;
  1493. /*
  1494. * Free atslave allocated in at_dma_xlate()
  1495. */
  1496. kfree(chan->private);
  1497. chan->private = NULL;
  1498. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  1499. }
  1500. #ifdef CONFIG_OF
  1501. static bool at_dma_filter(struct dma_chan *chan, void *slave)
  1502. {
  1503. struct at_dma_slave *atslave = slave;
  1504. if (atslave->dma_dev == chan->device->dev) {
  1505. chan->private = atslave;
  1506. return true;
  1507. } else {
  1508. return false;
  1509. }
  1510. }
  1511. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1512. struct of_dma *of_dma)
  1513. {
  1514. struct dma_chan *chan;
  1515. struct at_dma_chan *atchan;
  1516. struct at_dma_slave *atslave;
  1517. dma_cap_mask_t mask;
  1518. unsigned int per_id;
  1519. struct platform_device *dmac_pdev;
  1520. if (dma_spec->args_count != 2)
  1521. return NULL;
  1522. dmac_pdev = of_find_device_by_node(dma_spec->np);
  1523. if (!dmac_pdev)
  1524. return NULL;
  1525. dma_cap_zero(mask);
  1526. dma_cap_set(DMA_SLAVE, mask);
  1527. atslave = kmalloc(sizeof(*atslave), GFP_KERNEL);
  1528. if (!atslave) {
  1529. put_device(&dmac_pdev->dev);
  1530. return NULL;
  1531. }
  1532. atslave->cfg = ATC_DST_H2SEL | ATC_SRC_H2SEL;
  1533. /*
  1534. * We can fill both SRC_PER and DST_PER, one of these fields will be
  1535. * ignored depending on DMA transfer direction.
  1536. */
  1537. per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
  1538. atslave->cfg |= ATC_DST_PER_ID(per_id) | ATC_SRC_PER_ID(per_id);
  1539. /*
  1540. * We have to translate the value we get from the device tree since
  1541. * the half FIFO configuration value had to be 0 to keep backward
  1542. * compatibility.
  1543. */
  1544. switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
  1545. case AT91_DMA_CFG_FIFOCFG_ALAP:
  1546. atslave->cfg |= FIELD_PREP(ATC_FIFOCFG,
  1547. ATC_FIFOCFG_LARGESTBURST);
  1548. break;
  1549. case AT91_DMA_CFG_FIFOCFG_ASAP:
  1550. atslave->cfg |= FIELD_PREP(ATC_FIFOCFG,
  1551. ATC_FIFOCFG_ENOUGHSPACE);
  1552. break;
  1553. case AT91_DMA_CFG_FIFOCFG_HALF:
  1554. default:
  1555. atslave->cfg |= FIELD_PREP(ATC_FIFOCFG, ATC_FIFOCFG_HALFFIFO);
  1556. }
  1557. atslave->dma_dev = &dmac_pdev->dev;
  1558. chan = dma_request_channel(mask, at_dma_filter, atslave);
  1559. if (!chan) {
  1560. put_device(&dmac_pdev->dev);
  1561. kfree(atslave);
  1562. return NULL;
  1563. }
  1564. atchan = to_at_dma_chan(chan);
  1565. atchan->per_if = dma_spec->args[0] & 0xff;
  1566. atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
  1567. return chan;
  1568. }
  1569. #else
  1570. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1571. struct of_dma *of_dma)
  1572. {
  1573. return NULL;
  1574. }
  1575. #endif
  1576. /*-- Module Management -----------------------------------------------*/
  1577. /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
  1578. static struct at_dma_platform_data at91sam9rl_config = {
  1579. .nr_channels = 2,
  1580. };
  1581. static struct at_dma_platform_data at91sam9g45_config = {
  1582. .nr_channels = 8,
  1583. };
  1584. #if defined(CONFIG_OF)
  1585. static const struct of_device_id atmel_dma_dt_ids[] = {
  1586. {
  1587. .compatible = "atmel,at91sam9rl-dma",
  1588. .data = &at91sam9rl_config,
  1589. }, {
  1590. .compatible = "atmel,at91sam9g45-dma",
  1591. .data = &at91sam9g45_config,
  1592. }, {
  1593. /* sentinel */
  1594. }
  1595. };
  1596. MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
  1597. #endif
  1598. static const struct platform_device_id atdma_devtypes[] = {
  1599. {
  1600. .name = "at91sam9rl_dma",
  1601. .driver_data = (unsigned long) &at91sam9rl_config,
  1602. }, {
  1603. .name = "at91sam9g45_dma",
  1604. .driver_data = (unsigned long) &at91sam9g45_config,
  1605. }, {
  1606. /* sentinel */
  1607. }
  1608. };
  1609. static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
  1610. struct platform_device *pdev)
  1611. {
  1612. if (pdev->dev.of_node) {
  1613. const struct of_device_id *match;
  1614. match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
  1615. if (match == NULL)
  1616. return NULL;
  1617. return match->data;
  1618. }
  1619. return (struct at_dma_platform_data *)
  1620. platform_get_device_id(pdev)->driver_data;
  1621. }
  1622. /**
  1623. * at_dma_off - disable DMA controller
  1624. * @atdma: the Atmel HDAMC device
  1625. */
  1626. static void at_dma_off(struct at_dma *atdma)
  1627. {
  1628. dma_writel(atdma, EN, 0);
  1629. /* disable all interrupts */
  1630. dma_writel(atdma, EBCIDR, -1L);
  1631. /* confirm that all channels are disabled */
  1632. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  1633. cpu_relax();
  1634. }
  1635. static int __init at_dma_probe(struct platform_device *pdev)
  1636. {
  1637. struct at_dma *atdma;
  1638. int irq;
  1639. int err;
  1640. int i;
  1641. const struct at_dma_platform_data *plat_dat;
  1642. /* setup platform data for each SoC */
  1643. dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
  1644. dma_cap_set(DMA_INTERLEAVE, at91sam9g45_config.cap_mask);
  1645. dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
  1646. dma_cap_set(DMA_MEMSET, at91sam9g45_config.cap_mask);
  1647. dma_cap_set(DMA_MEMSET_SG, at91sam9g45_config.cap_mask);
  1648. dma_cap_set(DMA_PRIVATE, at91sam9g45_config.cap_mask);
  1649. dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
  1650. /* get DMA parameters from controller type */
  1651. plat_dat = at_dma_get_driver_data(pdev);
  1652. if (!plat_dat)
  1653. return -ENODEV;
  1654. atdma = devm_kzalloc(&pdev->dev,
  1655. struct_size(atdma, chan, plat_dat->nr_channels),
  1656. GFP_KERNEL);
  1657. if (!atdma)
  1658. return -ENOMEM;
  1659. atdma->regs = devm_platform_ioremap_resource(pdev, 0);
  1660. if (IS_ERR(atdma->regs))
  1661. return PTR_ERR(atdma->regs);
  1662. irq = platform_get_irq(pdev, 0);
  1663. if (irq < 0)
  1664. return irq;
  1665. /* discover transaction capabilities */
  1666. atdma->dma_device.cap_mask = plat_dat->cap_mask;
  1667. atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
  1668. atdma->clk = devm_clk_get(&pdev->dev, "dma_clk");
  1669. if (IS_ERR(atdma->clk))
  1670. return PTR_ERR(atdma->clk);
  1671. err = clk_prepare_enable(atdma->clk);
  1672. if (err)
  1673. return err;
  1674. /* force dma off, just in case */
  1675. at_dma_off(atdma);
  1676. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  1677. if (err)
  1678. goto err_irq;
  1679. platform_set_drvdata(pdev, atdma);
  1680. /* create a pool of consistent memory blocks for hardware descriptors */
  1681. atdma->lli_pool = dma_pool_create("at_hdmac_lli_pool",
  1682. &pdev->dev, sizeof(struct at_lli),
  1683. 4 /* word alignment */, 0);
  1684. if (!atdma->lli_pool) {
  1685. dev_err(&pdev->dev, "Unable to allocate DMA LLI descriptor pool\n");
  1686. err = -ENOMEM;
  1687. goto err_desc_pool_create;
  1688. }
  1689. /* create a pool of consistent memory blocks for memset blocks */
  1690. atdma->memset_pool = dma_pool_create("at_hdmac_memset_pool",
  1691. &pdev->dev, sizeof(int), 4, 0);
  1692. if (!atdma->memset_pool) {
  1693. dev_err(&pdev->dev, "No memory for memset dma pool\n");
  1694. err = -ENOMEM;
  1695. goto err_memset_pool_create;
  1696. }
  1697. /* clear any pending interrupt */
  1698. while (dma_readl(atdma, EBCISR))
  1699. cpu_relax();
  1700. /* initialize channels related values */
  1701. INIT_LIST_HEAD(&atdma->dma_device.channels);
  1702. for (i = 0; i < plat_dat->nr_channels; i++) {
  1703. struct at_dma_chan *atchan = &atdma->chan[i];
  1704. atchan->mem_if = AT_DMA_MEM_IF;
  1705. atchan->per_if = AT_DMA_PER_IF;
  1706. atchan->ch_regs = atdma->regs + ch_regs(i);
  1707. atchan->mask = 1 << i;
  1708. atchan->atdma = atdma;
  1709. atchan->vc.desc_free = atdma_desc_free;
  1710. vchan_init(&atchan->vc, &atdma->dma_device);
  1711. atc_enable_chan_irq(atdma, i);
  1712. }
  1713. /* set base routines */
  1714. atdma->dma_device.device_alloc_chan_resources = atc_alloc_chan_resources;
  1715. atdma->dma_device.device_free_chan_resources = atc_free_chan_resources;
  1716. atdma->dma_device.device_tx_status = atc_tx_status;
  1717. atdma->dma_device.device_issue_pending = atc_issue_pending;
  1718. atdma->dma_device.dev = &pdev->dev;
  1719. /* set prep routines based on capability */
  1720. if (dma_has_cap(DMA_INTERLEAVE, atdma->dma_device.cap_mask))
  1721. atdma->dma_device.device_prep_interleaved_dma = atc_prep_dma_interleaved;
  1722. if (dma_has_cap(DMA_MEMCPY, atdma->dma_device.cap_mask))
  1723. atdma->dma_device.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  1724. if (dma_has_cap(DMA_MEMSET, atdma->dma_device.cap_mask)) {
  1725. atdma->dma_device.device_prep_dma_memset = atc_prep_dma_memset;
  1726. atdma->dma_device.device_prep_dma_memset_sg = atc_prep_dma_memset_sg;
  1727. atdma->dma_device.fill_align = DMAENGINE_ALIGN_4_BYTES;
  1728. }
  1729. if (dma_has_cap(DMA_SLAVE, atdma->dma_device.cap_mask)) {
  1730. atdma->dma_device.device_prep_slave_sg = atc_prep_slave_sg;
  1731. /* controller can do slave DMA: can trigger cyclic transfers */
  1732. dma_cap_set(DMA_CYCLIC, atdma->dma_device.cap_mask);
  1733. atdma->dma_device.device_prep_dma_cyclic = atc_prep_dma_cyclic;
  1734. atdma->dma_device.device_config = atc_config;
  1735. atdma->dma_device.device_pause = atc_pause;
  1736. atdma->dma_device.device_resume = atc_resume;
  1737. atdma->dma_device.device_terminate_all = atc_terminate_all;
  1738. atdma->dma_device.src_addr_widths = ATC_DMA_BUSWIDTHS;
  1739. atdma->dma_device.dst_addr_widths = ATC_DMA_BUSWIDTHS;
  1740. atdma->dma_device.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  1741. atdma->dma_device.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1742. }
  1743. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1744. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s%s), %d channels\n",
  1745. dma_has_cap(DMA_MEMCPY, atdma->dma_device.cap_mask) ? "cpy " : "",
  1746. dma_has_cap(DMA_MEMSET, atdma->dma_device.cap_mask) ? "set " : "",
  1747. dma_has_cap(DMA_SLAVE, atdma->dma_device.cap_mask) ? "slave " : "",
  1748. plat_dat->nr_channels);
  1749. err = dma_async_device_register(&atdma->dma_device);
  1750. if (err) {
  1751. dev_err(&pdev->dev, "Unable to register: %d.\n", err);
  1752. goto err_dma_async_device_register;
  1753. }
  1754. /*
  1755. * Do not return an error if the dmac node is not present in order to
  1756. * not break the existing way of requesting channel with
  1757. * dma_request_channel().
  1758. */
  1759. if (pdev->dev.of_node) {
  1760. err = of_dma_controller_register(pdev->dev.of_node,
  1761. at_dma_xlate, atdma);
  1762. if (err) {
  1763. dev_err(&pdev->dev, "could not register of_dma_controller\n");
  1764. goto err_of_dma_controller_register;
  1765. }
  1766. }
  1767. return 0;
  1768. err_of_dma_controller_register:
  1769. dma_async_device_unregister(&atdma->dma_device);
  1770. err_dma_async_device_register:
  1771. dma_pool_destroy(atdma->memset_pool);
  1772. err_memset_pool_create:
  1773. dma_pool_destroy(atdma->lli_pool);
  1774. err_desc_pool_create:
  1775. free_irq(platform_get_irq(pdev, 0), atdma);
  1776. err_irq:
  1777. clk_disable_unprepare(atdma->clk);
  1778. return err;
  1779. }
  1780. static void at_dma_remove(struct platform_device *pdev)
  1781. {
  1782. struct at_dma *atdma = platform_get_drvdata(pdev);
  1783. struct dma_chan *chan, *_chan;
  1784. at_dma_off(atdma);
  1785. if (pdev->dev.of_node)
  1786. of_dma_controller_free(pdev->dev.of_node);
  1787. dma_async_device_unregister(&atdma->dma_device);
  1788. dma_pool_destroy(atdma->memset_pool);
  1789. dma_pool_destroy(atdma->lli_pool);
  1790. free_irq(platform_get_irq(pdev, 0), atdma);
  1791. list_for_each_entry_safe(chan, _chan, &atdma->dma_device.channels,
  1792. device_node) {
  1793. /* Disable interrupts */
  1794. atc_disable_chan_irq(atdma, chan->chan_id);
  1795. list_del(&chan->device_node);
  1796. }
  1797. clk_disable_unprepare(atdma->clk);
  1798. }
  1799. static void at_dma_shutdown(struct platform_device *pdev)
  1800. {
  1801. struct at_dma *atdma = platform_get_drvdata(pdev);
  1802. at_dma_off(platform_get_drvdata(pdev));
  1803. clk_disable_unprepare(atdma->clk);
  1804. }
  1805. static int at_dma_prepare(struct device *dev)
  1806. {
  1807. struct at_dma *atdma = dev_get_drvdata(dev);
  1808. struct dma_chan *chan, *_chan;
  1809. list_for_each_entry_safe(chan, _chan, &atdma->dma_device.channels,
  1810. device_node) {
  1811. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1812. /* wait for transaction completion (except in cyclic case) */
  1813. if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
  1814. return -EAGAIN;
  1815. }
  1816. return 0;
  1817. }
  1818. static void atc_suspend_cyclic(struct at_dma_chan *atchan)
  1819. {
  1820. struct dma_chan *chan = &atchan->vc.chan;
  1821. /* Channel should be paused by user
  1822. * do it anyway even if it is not done already */
  1823. if (!atc_chan_is_paused(atchan)) {
  1824. dev_warn(chan2dev(chan),
  1825. "cyclic channel not paused, should be done by channel user\n");
  1826. atc_pause(chan);
  1827. }
  1828. /* now preserve additional data for cyclic operations */
  1829. /* next descriptor address in the cyclic list */
  1830. atchan->save_dscr = channel_readl(atchan, DSCR);
  1831. vdbg_dump_regs(atchan);
  1832. }
  1833. static int at_dma_suspend_noirq(struct device *dev)
  1834. {
  1835. struct at_dma *atdma = dev_get_drvdata(dev);
  1836. struct dma_chan *chan, *_chan;
  1837. /* preserve data */
  1838. list_for_each_entry_safe(chan, _chan, &atdma->dma_device.channels,
  1839. device_node) {
  1840. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1841. if (atc_chan_is_cyclic(atchan))
  1842. atc_suspend_cyclic(atchan);
  1843. atchan->save_cfg = channel_readl(atchan, CFG);
  1844. }
  1845. atdma->save_imr = dma_readl(atdma, EBCIMR);
  1846. /* disable DMA controller */
  1847. at_dma_off(atdma);
  1848. clk_disable_unprepare(atdma->clk);
  1849. return 0;
  1850. }
  1851. static void atc_resume_cyclic(struct at_dma_chan *atchan)
  1852. {
  1853. struct at_dma *atdma = to_at_dma(atchan->vc.chan.device);
  1854. /* restore channel status for cyclic descriptors list:
  1855. * next descriptor in the cyclic list at the time of suspend */
  1856. channel_writel(atchan, SADDR, 0);
  1857. channel_writel(atchan, DADDR, 0);
  1858. channel_writel(atchan, CTRLA, 0);
  1859. channel_writel(atchan, CTRLB, 0);
  1860. channel_writel(atchan, DSCR, atchan->save_dscr);
  1861. dma_writel(atdma, CHER, atchan->mask);
  1862. /* channel pause status should be removed by channel user
  1863. * We cannot take the initiative to do it here */
  1864. vdbg_dump_regs(atchan);
  1865. }
  1866. static int at_dma_resume_noirq(struct device *dev)
  1867. {
  1868. struct at_dma *atdma = dev_get_drvdata(dev);
  1869. struct dma_chan *chan, *_chan;
  1870. /* bring back DMA controller */
  1871. clk_prepare_enable(atdma->clk);
  1872. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1873. /* clear any pending interrupt */
  1874. while (dma_readl(atdma, EBCISR))
  1875. cpu_relax();
  1876. /* restore saved data */
  1877. dma_writel(atdma, EBCIER, atdma->save_imr);
  1878. list_for_each_entry_safe(chan, _chan, &atdma->dma_device.channels,
  1879. device_node) {
  1880. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1881. channel_writel(atchan, CFG, atchan->save_cfg);
  1882. if (atc_chan_is_cyclic(atchan))
  1883. atc_resume_cyclic(atchan);
  1884. }
  1885. return 0;
  1886. }
  1887. static const struct dev_pm_ops __maybe_unused at_dma_dev_pm_ops = {
  1888. .prepare = at_dma_prepare,
  1889. .suspend_noirq = at_dma_suspend_noirq,
  1890. .resume_noirq = at_dma_resume_noirq,
  1891. };
  1892. static struct platform_driver at_dma_driver = {
  1893. .remove_new = at_dma_remove,
  1894. .shutdown = at_dma_shutdown,
  1895. .id_table = atdma_devtypes,
  1896. .driver = {
  1897. .name = "at_hdmac",
  1898. .pm = pm_ptr(&at_dma_dev_pm_ops),
  1899. .of_match_table = of_match_ptr(atmel_dma_dt_ids),
  1900. },
  1901. };
  1902. static int __init at_dma_init(void)
  1903. {
  1904. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1905. }
  1906. subsys_initcall(at_dma_init);
  1907. static void __exit at_dma_exit(void)
  1908. {
  1909. platform_driver_unregister(&at_dma_driver);
  1910. }
  1911. module_exit(at_dma_exit);
  1912. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1913. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1914. MODULE_AUTHOR("Tudor Ambarus <tudor.ambarus@microchip.com>");
  1915. MODULE_LICENSE("GPL");
  1916. MODULE_ALIAS("platform:at_hdmac");