at_hdmac.c 62 KB

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