at_hdmac.c 59 KB

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