at_hdmac.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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. /*
  42. * Initial number of descriptors to allocate for each channel. This could
  43. * be increased during dma usage.
  44. */
  45. static unsigned int init_nr_desc_per_channel = 64;
  46. module_param(init_nr_desc_per_channel, uint, 0644);
  47. MODULE_PARM_DESC(init_nr_desc_per_channel,
  48. "initial descriptors per channel (default: 64)");
  49. /* prototypes */
  50. static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx);
  51. static void atc_issue_pending(struct dma_chan *chan);
  52. /*----------------------------------------------------------------------*/
  53. static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
  54. {
  55. return list_first_entry(&atchan->active_list,
  56. struct at_desc, desc_node);
  57. }
  58. static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
  59. {
  60. return list_first_entry(&atchan->queue,
  61. struct at_desc, desc_node);
  62. }
  63. /**
  64. * atc_alloc_descriptor - allocate and return an initialized descriptor
  65. * @chan: the channel to allocate descriptors for
  66. * @gfp_flags: GFP allocation flags
  67. *
  68. * Note: The ack-bit is positioned in the descriptor flag at creation time
  69. * to make initial allocation more convenient. This bit will be cleared
  70. * and control will be given to client at usage time (during
  71. * preparation functions).
  72. */
  73. static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
  74. gfp_t gfp_flags)
  75. {
  76. struct at_desc *desc = NULL;
  77. struct at_dma *atdma = to_at_dma(chan->device);
  78. dma_addr_t phys;
  79. desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
  80. if (desc) {
  81. memset(desc, 0, sizeof(struct at_desc));
  82. INIT_LIST_HEAD(&desc->tx_list);
  83. dma_async_tx_descriptor_init(&desc->txd, chan);
  84. /* txd.flags will be overwritten in prep functions */
  85. desc->txd.flags = DMA_CTRL_ACK;
  86. desc->txd.tx_submit = atc_tx_submit;
  87. desc->txd.phys = phys;
  88. }
  89. return desc;
  90. }
  91. /**
  92. * atc_desc_get - get an unused descriptor from free_list
  93. * @atchan: channel we want a new descriptor for
  94. */
  95. static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
  96. {
  97. struct at_desc *desc, *_desc;
  98. struct at_desc *ret = NULL;
  99. unsigned long flags;
  100. unsigned int i = 0;
  101. LIST_HEAD(tmp_list);
  102. spin_lock_irqsave(&atchan->lock, flags);
  103. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  104. i++;
  105. if (async_tx_test_ack(&desc->txd)) {
  106. list_del(&desc->desc_node);
  107. ret = desc;
  108. break;
  109. }
  110. dev_dbg(chan2dev(&atchan->chan_common),
  111. "desc %p not ACKed\n", desc);
  112. }
  113. spin_unlock_irqrestore(&atchan->lock, flags);
  114. dev_vdbg(chan2dev(&atchan->chan_common),
  115. "scanned %u descriptors on freelist\n", i);
  116. /* no more descriptor available in initial pool: create one more */
  117. if (!ret) {
  118. ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
  119. if (ret) {
  120. spin_lock_irqsave(&atchan->lock, flags);
  121. atchan->descs_allocated++;
  122. spin_unlock_irqrestore(&atchan->lock, flags);
  123. } else {
  124. dev_err(chan2dev(&atchan->chan_common),
  125. "not enough descriptors available\n");
  126. }
  127. }
  128. return ret;
  129. }
  130. /**
  131. * atc_desc_put - move a descriptor, including any children, to the free list
  132. * @atchan: channel we work on
  133. * @desc: descriptor, at the head of a chain, to move to free list
  134. */
  135. static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
  136. {
  137. if (desc) {
  138. struct at_desc *child;
  139. unsigned long flags;
  140. spin_lock_irqsave(&atchan->lock, flags);
  141. list_for_each_entry(child, &desc->tx_list, desc_node)
  142. dev_vdbg(chan2dev(&atchan->chan_common),
  143. "moving child desc %p to freelist\n",
  144. child);
  145. list_splice_init(&desc->tx_list, &atchan->free_list);
  146. dev_vdbg(chan2dev(&atchan->chan_common),
  147. "moving desc %p to freelist\n", desc);
  148. list_add(&desc->desc_node, &atchan->free_list);
  149. spin_unlock_irqrestore(&atchan->lock, flags);
  150. }
  151. }
  152. /**
  153. * atc_desc_chain - build chain adding a descriptor
  154. * @first: address of first descriptor of the chain
  155. * @prev: address of previous descriptor of the chain
  156. * @desc: descriptor to queue
  157. *
  158. * Called from prep_* functions
  159. */
  160. static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
  161. struct at_desc *desc)
  162. {
  163. if (!(*first)) {
  164. *first = desc;
  165. } else {
  166. /* inform the HW lli about chaining */
  167. (*prev)->lli.dscr = desc->txd.phys;
  168. /* insert the link descriptor to the LD ring */
  169. list_add_tail(&desc->desc_node,
  170. &(*first)->tx_list);
  171. }
  172. *prev = desc;
  173. }
  174. /**
  175. * atc_dostart - starts the DMA engine for real
  176. * @atchan: the channel we want to start
  177. * @first: first descriptor in the list we want to begin with
  178. *
  179. * Called with atchan->lock held and bh disabled
  180. */
  181. static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
  182. {
  183. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  184. /* ASSERT: channel is idle */
  185. if (atc_chan_is_enabled(atchan)) {
  186. dev_err(chan2dev(&atchan->chan_common),
  187. "BUG: Attempted to start non-idle channel\n");
  188. dev_err(chan2dev(&atchan->chan_common),
  189. " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
  190. channel_readl(atchan, SADDR),
  191. channel_readl(atchan, DADDR),
  192. channel_readl(atchan, CTRLA),
  193. channel_readl(atchan, CTRLB),
  194. channel_readl(atchan, DSCR));
  195. /* The tasklet will hopefully advance the queue... */
  196. return;
  197. }
  198. vdbg_dump_regs(atchan);
  199. channel_writel(atchan, SADDR, 0);
  200. channel_writel(atchan, DADDR, 0);
  201. channel_writel(atchan, CTRLA, 0);
  202. channel_writel(atchan, CTRLB, 0);
  203. channel_writel(atchan, DSCR, first->txd.phys);
  204. dma_writel(atdma, CHER, atchan->mask);
  205. vdbg_dump_regs(atchan);
  206. }
  207. /*
  208. * atc_get_current_descriptors -
  209. * locate the descriptor which equal to physical address in DSCR
  210. * @atchan: the channel we want to start
  211. * @dscr_addr: physical descriptor address in DSCR
  212. */
  213. static struct at_desc *atc_get_current_descriptors(struct at_dma_chan *atchan,
  214. u32 dscr_addr)
  215. {
  216. struct at_desc *desc, *_desc, *child, *desc_cur = NULL;
  217. list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
  218. if (desc->lli.dscr == dscr_addr) {
  219. desc_cur = desc;
  220. break;
  221. }
  222. list_for_each_entry(child, &desc->tx_list, desc_node) {
  223. if (child->lli.dscr == dscr_addr) {
  224. desc_cur = child;
  225. break;
  226. }
  227. }
  228. }
  229. return desc_cur;
  230. }
  231. /*
  232. * atc_get_bytes_left -
  233. * Get the number of bytes residue in dma buffer,
  234. * @chan: the channel we want to start
  235. */
  236. static int atc_get_bytes_left(struct dma_chan *chan)
  237. {
  238. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  239. struct at_dma *atdma = to_at_dma(chan->device);
  240. int chan_id = atchan->chan_common.chan_id;
  241. struct at_desc *desc_first = atc_first_active(atchan);
  242. struct at_desc *desc_cur;
  243. int ret = 0, count = 0;
  244. /*
  245. * Initialize necessary values in the first time.
  246. * remain_desc record remain desc length.
  247. */
  248. if (atchan->remain_desc == 0)
  249. /* First descriptor embedds the transaction length */
  250. atchan->remain_desc = desc_first->len;
  251. /*
  252. * This happens when current descriptor transfer complete.
  253. * The residual buffer size should reduce current descriptor length.
  254. */
  255. if (unlikely(test_bit(ATC_IS_BTC, &atchan->status))) {
  256. clear_bit(ATC_IS_BTC, &atchan->status);
  257. desc_cur = atc_get_current_descriptors(atchan,
  258. channel_readl(atchan, DSCR));
  259. if (!desc_cur) {
  260. ret = -EINVAL;
  261. goto out;
  262. }
  263. atchan->remain_desc -= (desc_cur->lli.ctrla & ATC_BTSIZE_MAX)
  264. << (desc_first->tx_width);
  265. if (atchan->remain_desc < 0) {
  266. ret = -EINVAL;
  267. goto out;
  268. } else {
  269. ret = atchan->remain_desc;
  270. }
  271. } else {
  272. /*
  273. * Get residual bytes when current
  274. * descriptor transfer in progress.
  275. */
  276. count = (channel_readl(atchan, CTRLA) & ATC_BTSIZE_MAX)
  277. << (desc_first->tx_width);
  278. ret = atchan->remain_desc - count;
  279. }
  280. /*
  281. * Check fifo empty.
  282. */
  283. if (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id)))
  284. atc_issue_pending(chan);
  285. out:
  286. return ret;
  287. }
  288. /**
  289. * atc_chain_complete - finish work for one transaction chain
  290. * @atchan: channel we work on
  291. * @desc: descriptor at the head of the chain we want do complete
  292. *
  293. * Called with atchan->lock held and bh disabled */
  294. static void
  295. atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
  296. {
  297. struct dma_async_tx_descriptor *txd = &desc->txd;
  298. dev_vdbg(chan2dev(&atchan->chan_common),
  299. "descriptor %u complete\n", txd->cookie);
  300. /* mark the descriptor as complete for non cyclic cases only */
  301. if (!atc_chan_is_cyclic(atchan))
  302. dma_cookie_complete(txd);
  303. /* move children to free_list */
  304. list_splice_init(&desc->tx_list, &atchan->free_list);
  305. /* move myself to free_list */
  306. list_move(&desc->desc_node, &atchan->free_list);
  307. dma_descriptor_unmap(txd);
  308. /* for cyclic transfers,
  309. * no need to replay callback function while stopping */
  310. if (!atc_chan_is_cyclic(atchan)) {
  311. dma_async_tx_callback callback = txd->callback;
  312. void *param = txd->callback_param;
  313. /*
  314. * The API requires that no submissions are done from a
  315. * callback, so we don't need to drop the lock here
  316. */
  317. if (callback)
  318. callback(param);
  319. }
  320. dma_run_dependencies(txd);
  321. }
  322. /**
  323. * atc_complete_all - finish work for all transactions
  324. * @atchan: channel to complete transactions for
  325. *
  326. * Eventually submit queued descriptors if any
  327. *
  328. * Assume channel is idle while calling this function
  329. * Called with atchan->lock held and bh disabled
  330. */
  331. static void atc_complete_all(struct at_dma_chan *atchan)
  332. {
  333. struct at_desc *desc, *_desc;
  334. LIST_HEAD(list);
  335. dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
  336. /*
  337. * Submit queued descriptors ASAP, i.e. before we go through
  338. * the completed ones.
  339. */
  340. if (!list_empty(&atchan->queue))
  341. atc_dostart(atchan, atc_first_queued(atchan));
  342. /* empty active_list now it is completed */
  343. list_splice_init(&atchan->active_list, &list);
  344. /* empty queue list by moving descriptors (if any) to active_list */
  345. list_splice_init(&atchan->queue, &atchan->active_list);
  346. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  347. atc_chain_complete(atchan, desc);
  348. }
  349. /**
  350. * atc_advance_work - at the end of a transaction, move forward
  351. * @atchan: channel where the transaction ended
  352. *
  353. * Called with atchan->lock held and bh disabled
  354. */
  355. static void atc_advance_work(struct at_dma_chan *atchan)
  356. {
  357. dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
  358. if (atc_chan_is_enabled(atchan))
  359. return;
  360. if (list_empty(&atchan->active_list) ||
  361. list_is_singular(&atchan->active_list)) {
  362. atc_complete_all(atchan);
  363. } else {
  364. atc_chain_complete(atchan, atc_first_active(atchan));
  365. /* advance work */
  366. atc_dostart(atchan, atc_first_active(atchan));
  367. }
  368. }
  369. /**
  370. * atc_handle_error - handle errors reported by DMA controller
  371. * @atchan: channel where error occurs
  372. *
  373. * Called with atchan->lock held and bh disabled
  374. */
  375. static void atc_handle_error(struct at_dma_chan *atchan)
  376. {
  377. struct at_desc *bad_desc;
  378. struct at_desc *child;
  379. /*
  380. * The descriptor currently at the head of the active list is
  381. * broked. Since we don't have any way to report errors, we'll
  382. * just have to scream loudly and try to carry on.
  383. */
  384. bad_desc = atc_first_active(atchan);
  385. list_del_init(&bad_desc->desc_node);
  386. /* As we are stopped, take advantage to push queued descriptors
  387. * in active_list */
  388. list_splice_init(&atchan->queue, atchan->active_list.prev);
  389. /* Try to restart the controller */
  390. if (!list_empty(&atchan->active_list))
  391. atc_dostart(atchan, atc_first_active(atchan));
  392. /*
  393. * KERN_CRITICAL may seem harsh, but since this only happens
  394. * when someone submits a bad physical address in a
  395. * descriptor, we should consider ourselves lucky that the
  396. * controller flagged an error instead of scribbling over
  397. * random memory locations.
  398. */
  399. dev_crit(chan2dev(&atchan->chan_common),
  400. "Bad descriptor submitted for DMA!\n");
  401. dev_crit(chan2dev(&atchan->chan_common),
  402. " cookie: %d\n", bad_desc->txd.cookie);
  403. atc_dump_lli(atchan, &bad_desc->lli);
  404. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  405. atc_dump_lli(atchan, &child->lli);
  406. /* Pretend the descriptor completed successfully */
  407. atc_chain_complete(atchan, bad_desc);
  408. }
  409. /**
  410. * atc_handle_cyclic - at the end of a period, run callback function
  411. * @atchan: channel used for cyclic operations
  412. *
  413. * Called with atchan->lock held and bh disabled
  414. */
  415. static void atc_handle_cyclic(struct at_dma_chan *atchan)
  416. {
  417. struct at_desc *first = atc_first_active(atchan);
  418. struct dma_async_tx_descriptor *txd = &first->txd;
  419. dma_async_tx_callback callback = txd->callback;
  420. void *param = txd->callback_param;
  421. dev_vdbg(chan2dev(&atchan->chan_common),
  422. "new cyclic period llp 0x%08x\n",
  423. channel_readl(atchan, DSCR));
  424. if (callback)
  425. callback(param);
  426. }
  427. /*-- IRQ & Tasklet ---------------------------------------------------*/
  428. static void atc_tasklet(unsigned long data)
  429. {
  430. struct at_dma_chan *atchan = (struct at_dma_chan *)data;
  431. unsigned long flags;
  432. spin_lock_irqsave(&atchan->lock, flags);
  433. if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
  434. atc_handle_error(atchan);
  435. else if (atc_chan_is_cyclic(atchan))
  436. atc_handle_cyclic(atchan);
  437. else
  438. atc_advance_work(atchan);
  439. spin_unlock_irqrestore(&atchan->lock, flags);
  440. }
  441. static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
  442. {
  443. struct at_dma *atdma = (struct at_dma *)dev_id;
  444. struct at_dma_chan *atchan;
  445. int i;
  446. u32 status, pending, imr;
  447. int ret = IRQ_NONE;
  448. do {
  449. imr = dma_readl(atdma, EBCIMR);
  450. status = dma_readl(atdma, EBCISR);
  451. pending = status & imr;
  452. if (!pending)
  453. break;
  454. dev_vdbg(atdma->dma_common.dev,
  455. "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
  456. status, imr, pending);
  457. for (i = 0; i < atdma->dma_common.chancnt; i++) {
  458. atchan = &atdma->chan[i];
  459. if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
  460. if (pending & AT_DMA_ERR(i)) {
  461. /* Disable channel on AHB error */
  462. dma_writel(atdma, CHDR,
  463. AT_DMA_RES(i) | atchan->mask);
  464. /* Give information to tasklet */
  465. set_bit(ATC_IS_ERROR, &atchan->status);
  466. }
  467. if (pending & AT_DMA_BTC(i))
  468. set_bit(ATC_IS_BTC, &atchan->status);
  469. tasklet_schedule(&atchan->tasklet);
  470. ret = IRQ_HANDLED;
  471. }
  472. }
  473. } while (pending);
  474. return ret;
  475. }
  476. /*-- DMA Engine API --------------------------------------------------*/
  477. /**
  478. * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
  479. * @desc: descriptor at the head of the transaction chain
  480. *
  481. * Queue chain if DMA engine is working already
  482. *
  483. * Cookie increment and adding to active_list or queue must be atomic
  484. */
  485. static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
  486. {
  487. struct at_desc *desc = txd_to_at_desc(tx);
  488. struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
  489. dma_cookie_t cookie;
  490. unsigned long flags;
  491. spin_lock_irqsave(&atchan->lock, flags);
  492. cookie = dma_cookie_assign(tx);
  493. if (list_empty(&atchan->active_list)) {
  494. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  495. desc->txd.cookie);
  496. atc_dostart(atchan, desc);
  497. list_add_tail(&desc->desc_node, &atchan->active_list);
  498. } else {
  499. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  500. desc->txd.cookie);
  501. list_add_tail(&desc->desc_node, &atchan->queue);
  502. }
  503. spin_unlock_irqrestore(&atchan->lock, flags);
  504. return cookie;
  505. }
  506. /**
  507. * atc_prep_dma_memcpy - prepare a memcpy operation
  508. * @chan: the channel to prepare operation on
  509. * @dest: operation virtual destination address
  510. * @src: operation virtual source address
  511. * @len: operation length
  512. * @flags: tx descriptor status flags
  513. */
  514. static struct dma_async_tx_descriptor *
  515. atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  516. size_t len, unsigned long flags)
  517. {
  518. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  519. struct at_desc *desc = NULL;
  520. struct at_desc *first = NULL;
  521. struct at_desc *prev = NULL;
  522. size_t xfer_count;
  523. size_t offset;
  524. unsigned int src_width;
  525. unsigned int dst_width;
  526. u32 ctrla;
  527. u32 ctrlb;
  528. dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
  529. dest, src, len, flags);
  530. if (unlikely(!len)) {
  531. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  532. return NULL;
  533. }
  534. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  535. | ATC_SRC_ADDR_MODE_INCR
  536. | ATC_DST_ADDR_MODE_INCR
  537. | ATC_FC_MEM2MEM;
  538. /*
  539. * We can be a lot more clever here, but this should take care
  540. * of the most common optimization.
  541. */
  542. if (!((src | dest | len) & 3)) {
  543. ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
  544. src_width = dst_width = 2;
  545. } else if (!((src | dest | len) & 1)) {
  546. ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
  547. src_width = dst_width = 1;
  548. } else {
  549. ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
  550. src_width = dst_width = 0;
  551. }
  552. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  553. xfer_count = min_t(size_t, (len - offset) >> src_width,
  554. ATC_BTSIZE_MAX);
  555. desc = atc_desc_get(atchan);
  556. if (!desc)
  557. goto err_desc_get;
  558. desc->lli.saddr = src + offset;
  559. desc->lli.daddr = dest + offset;
  560. desc->lli.ctrla = ctrla | xfer_count;
  561. desc->lli.ctrlb = ctrlb;
  562. desc->txd.cookie = 0;
  563. atc_desc_chain(&first, &prev, desc);
  564. }
  565. /* First descriptor of the chain embedds additional information */
  566. first->txd.cookie = -EBUSY;
  567. first->len = len;
  568. first->tx_width = src_width;
  569. /* set end-of-link to the last link descriptor of list*/
  570. set_desc_eol(desc);
  571. first->txd.flags = flags; /* client is in control of this ack */
  572. return &first->txd;
  573. err_desc_get:
  574. atc_desc_put(atchan, first);
  575. return NULL;
  576. }
  577. /**
  578. * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  579. * @chan: DMA channel
  580. * @sgl: scatterlist to transfer to/from
  581. * @sg_len: number of entries in @scatterlist
  582. * @direction: DMA direction
  583. * @flags: tx descriptor status flags
  584. * @context: transaction context (ignored)
  585. */
  586. static struct dma_async_tx_descriptor *
  587. atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  588. unsigned int sg_len, enum dma_transfer_direction direction,
  589. unsigned long flags, void *context)
  590. {
  591. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  592. struct at_dma_slave *atslave = chan->private;
  593. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  594. struct at_desc *first = NULL;
  595. struct at_desc *prev = NULL;
  596. u32 ctrla;
  597. u32 ctrlb;
  598. dma_addr_t reg;
  599. unsigned int reg_width;
  600. unsigned int mem_width;
  601. unsigned int i;
  602. struct scatterlist *sg;
  603. size_t total_len = 0;
  604. dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
  605. sg_len,
  606. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  607. flags);
  608. if (unlikely(!atslave || !sg_len)) {
  609. dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
  610. return NULL;
  611. }
  612. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  613. | ATC_DCSIZE(sconfig->dst_maxburst);
  614. ctrlb = ATC_IEN;
  615. switch (direction) {
  616. case DMA_MEM_TO_DEV:
  617. reg_width = convert_buswidth(sconfig->dst_addr_width);
  618. ctrla |= ATC_DST_WIDTH(reg_width);
  619. ctrlb |= ATC_DST_ADDR_MODE_FIXED
  620. | ATC_SRC_ADDR_MODE_INCR
  621. | ATC_FC_MEM2PER
  622. | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
  623. reg = sconfig->dst_addr;
  624. for_each_sg(sgl, sg, sg_len, i) {
  625. struct at_desc *desc;
  626. u32 len;
  627. u32 mem;
  628. desc = atc_desc_get(atchan);
  629. if (!desc)
  630. goto err_desc_get;
  631. mem = sg_dma_address(sg);
  632. len = sg_dma_len(sg);
  633. if (unlikely(!len)) {
  634. dev_dbg(chan2dev(chan),
  635. "prep_slave_sg: sg(%d) data length is zero\n", i);
  636. goto err;
  637. }
  638. mem_width = 2;
  639. if (unlikely(mem & 3 || len & 3))
  640. mem_width = 0;
  641. desc->lli.saddr = mem;
  642. desc->lli.daddr = reg;
  643. desc->lli.ctrla = ctrla
  644. | ATC_SRC_WIDTH(mem_width)
  645. | len >> mem_width;
  646. desc->lli.ctrlb = ctrlb;
  647. atc_desc_chain(&first, &prev, desc);
  648. total_len += len;
  649. }
  650. break;
  651. case DMA_DEV_TO_MEM:
  652. reg_width = convert_buswidth(sconfig->src_addr_width);
  653. ctrla |= ATC_SRC_WIDTH(reg_width);
  654. ctrlb |= ATC_DST_ADDR_MODE_INCR
  655. | ATC_SRC_ADDR_MODE_FIXED
  656. | ATC_FC_PER2MEM
  657. | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
  658. reg = sconfig->src_addr;
  659. for_each_sg(sgl, sg, sg_len, i) {
  660. struct at_desc *desc;
  661. u32 len;
  662. u32 mem;
  663. desc = atc_desc_get(atchan);
  664. if (!desc)
  665. goto err_desc_get;
  666. mem = sg_dma_address(sg);
  667. len = sg_dma_len(sg);
  668. if (unlikely(!len)) {
  669. dev_dbg(chan2dev(chan),
  670. "prep_slave_sg: sg(%d) data length is zero\n", i);
  671. goto err;
  672. }
  673. mem_width = 2;
  674. if (unlikely(mem & 3 || len & 3))
  675. mem_width = 0;
  676. desc->lli.saddr = reg;
  677. desc->lli.daddr = mem;
  678. desc->lli.ctrla = ctrla
  679. | ATC_DST_WIDTH(mem_width)
  680. | len >> reg_width;
  681. desc->lli.ctrlb = ctrlb;
  682. atc_desc_chain(&first, &prev, desc);
  683. total_len += len;
  684. }
  685. break;
  686. default:
  687. return NULL;
  688. }
  689. /* set end-of-link to the last link descriptor of list*/
  690. set_desc_eol(prev);
  691. /* First descriptor of the chain embedds additional information */
  692. first->txd.cookie = -EBUSY;
  693. first->len = total_len;
  694. first->tx_width = reg_width;
  695. /* first link descriptor of list is responsible of flags */
  696. first->txd.flags = flags; /* client is in control of this ack */
  697. return &first->txd;
  698. err_desc_get:
  699. dev_err(chan2dev(chan), "not enough descriptors available\n");
  700. err:
  701. atc_desc_put(atchan, first);
  702. return NULL;
  703. }
  704. /**
  705. * atc_dma_cyclic_check_values
  706. * Check for too big/unaligned periods and unaligned DMA buffer
  707. */
  708. static int
  709. atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
  710. size_t period_len)
  711. {
  712. if (period_len > (ATC_BTSIZE_MAX << reg_width))
  713. goto err_out;
  714. if (unlikely(period_len & ((1 << reg_width) - 1)))
  715. goto err_out;
  716. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  717. goto err_out;
  718. return 0;
  719. err_out:
  720. return -EINVAL;
  721. }
  722. /**
  723. * atc_dma_cyclic_fill_desc - Fill one period descriptor
  724. */
  725. static int
  726. atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
  727. unsigned int period_index, dma_addr_t buf_addr,
  728. unsigned int reg_width, size_t period_len,
  729. enum dma_transfer_direction direction)
  730. {
  731. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  732. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  733. u32 ctrla;
  734. /* prepare common CRTLA value */
  735. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  736. | ATC_DCSIZE(sconfig->dst_maxburst)
  737. | ATC_DST_WIDTH(reg_width)
  738. | ATC_SRC_WIDTH(reg_width)
  739. | period_len >> reg_width;
  740. switch (direction) {
  741. case DMA_MEM_TO_DEV:
  742. desc->lli.saddr = buf_addr + (period_len * period_index);
  743. desc->lli.daddr = sconfig->dst_addr;
  744. desc->lli.ctrla = ctrla;
  745. desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
  746. | ATC_SRC_ADDR_MODE_INCR
  747. | ATC_FC_MEM2PER
  748. | ATC_SIF(atchan->mem_if)
  749. | ATC_DIF(atchan->per_if);
  750. break;
  751. case DMA_DEV_TO_MEM:
  752. desc->lli.saddr = sconfig->src_addr;
  753. desc->lli.daddr = buf_addr + (period_len * period_index);
  754. desc->lli.ctrla = ctrla;
  755. desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
  756. | ATC_SRC_ADDR_MODE_FIXED
  757. | ATC_FC_PER2MEM
  758. | ATC_SIF(atchan->per_if)
  759. | ATC_DIF(atchan->mem_if);
  760. break;
  761. default:
  762. return -EINVAL;
  763. }
  764. return 0;
  765. }
  766. /**
  767. * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
  768. * @chan: the DMA channel to prepare
  769. * @buf_addr: physical DMA address where the buffer starts
  770. * @buf_len: total number of bytes for the entire buffer
  771. * @period_len: number of bytes for each period
  772. * @direction: transfer direction, to or from device
  773. * @flags: tx descriptor status flags
  774. * @context: transfer context (ignored)
  775. */
  776. static struct dma_async_tx_descriptor *
  777. atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  778. size_t period_len, enum dma_transfer_direction direction,
  779. unsigned long flags, void *context)
  780. {
  781. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  782. struct at_dma_slave *atslave = chan->private;
  783. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  784. struct at_desc *first = NULL;
  785. struct at_desc *prev = NULL;
  786. unsigned long was_cyclic;
  787. unsigned int reg_width;
  788. unsigned int periods = buf_len / period_len;
  789. unsigned int i;
  790. dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
  791. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  792. buf_addr,
  793. periods, buf_len, period_len);
  794. if (unlikely(!atslave || !buf_len || !period_len)) {
  795. dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
  796. return NULL;
  797. }
  798. was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
  799. if (was_cyclic) {
  800. dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
  801. return NULL;
  802. }
  803. if (unlikely(!is_slave_direction(direction)))
  804. goto err_out;
  805. if (sconfig->direction == DMA_MEM_TO_DEV)
  806. reg_width = convert_buswidth(sconfig->dst_addr_width);
  807. else
  808. reg_width = convert_buswidth(sconfig->src_addr_width);
  809. /* Check for too big/unaligned periods and unaligned DMA buffer */
  810. if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
  811. goto err_out;
  812. /* build cyclic linked list */
  813. for (i = 0; i < periods; i++) {
  814. struct at_desc *desc;
  815. desc = atc_desc_get(atchan);
  816. if (!desc)
  817. goto err_desc_get;
  818. if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
  819. reg_width, period_len, direction))
  820. goto err_desc_get;
  821. atc_desc_chain(&first, &prev, desc);
  822. }
  823. /* lets make a cyclic list */
  824. prev->lli.dscr = first->txd.phys;
  825. /* First descriptor of the chain embedds additional information */
  826. first->txd.cookie = -EBUSY;
  827. first->len = buf_len;
  828. first->tx_width = reg_width;
  829. return &first->txd;
  830. err_desc_get:
  831. dev_err(chan2dev(chan), "not enough descriptors available\n");
  832. atc_desc_put(atchan, first);
  833. err_out:
  834. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  835. return NULL;
  836. }
  837. static int set_runtime_config(struct dma_chan *chan,
  838. struct dma_slave_config *sconfig)
  839. {
  840. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  841. /* Check if it is chan is configured for slave transfers */
  842. if (!chan->private)
  843. return -EINVAL;
  844. memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
  845. convert_burst(&atchan->dma_sconfig.src_maxburst);
  846. convert_burst(&atchan->dma_sconfig.dst_maxburst);
  847. return 0;
  848. }
  849. static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  850. unsigned long arg)
  851. {
  852. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  853. struct at_dma *atdma = to_at_dma(chan->device);
  854. int chan_id = atchan->chan_common.chan_id;
  855. unsigned long flags;
  856. LIST_HEAD(list);
  857. dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
  858. if (cmd == DMA_PAUSE) {
  859. spin_lock_irqsave(&atchan->lock, flags);
  860. dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
  861. set_bit(ATC_IS_PAUSED, &atchan->status);
  862. spin_unlock_irqrestore(&atchan->lock, flags);
  863. } else if (cmd == DMA_RESUME) {
  864. if (!atc_chan_is_paused(atchan))
  865. return 0;
  866. spin_lock_irqsave(&atchan->lock, flags);
  867. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
  868. clear_bit(ATC_IS_PAUSED, &atchan->status);
  869. spin_unlock_irqrestore(&atchan->lock, flags);
  870. } else if (cmd == DMA_TERMINATE_ALL) {
  871. struct at_desc *desc, *_desc;
  872. /*
  873. * This is only called when something went wrong elsewhere, so
  874. * we don't really care about the data. Just disable the
  875. * channel. We still have to poll the channel enable bit due
  876. * to AHB/HSB limitations.
  877. */
  878. spin_lock_irqsave(&atchan->lock, flags);
  879. /* disabling channel: must also remove suspend state */
  880. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
  881. /* confirm that this channel is disabled */
  882. while (dma_readl(atdma, CHSR) & atchan->mask)
  883. cpu_relax();
  884. /* active_list entries will end up before queued entries */
  885. list_splice_init(&atchan->queue, &list);
  886. list_splice_init(&atchan->active_list, &list);
  887. /* Flush all pending and queued descriptors */
  888. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  889. atc_chain_complete(atchan, desc);
  890. clear_bit(ATC_IS_PAUSED, &atchan->status);
  891. /* if channel dedicated to cyclic operations, free it */
  892. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  893. spin_unlock_irqrestore(&atchan->lock, flags);
  894. } else if (cmd == DMA_SLAVE_CONFIG) {
  895. return set_runtime_config(chan, (struct dma_slave_config *)arg);
  896. } else {
  897. return -ENXIO;
  898. }
  899. return 0;
  900. }
  901. /**
  902. * atc_tx_status - poll for transaction completion
  903. * @chan: DMA channel
  904. * @cookie: transaction identifier to check status of
  905. * @txstate: if not %NULL updated with transaction state
  906. *
  907. * If @txstate is passed in, upon return it reflect the driver
  908. * internal state and can be used with dma_async_is_complete() to check
  909. * the status of multiple cookies without re-checking hardware state.
  910. */
  911. static enum dma_status
  912. atc_tx_status(struct dma_chan *chan,
  913. dma_cookie_t cookie,
  914. struct dma_tx_state *txstate)
  915. {
  916. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  917. unsigned long flags;
  918. enum dma_status ret;
  919. int bytes = 0;
  920. ret = dma_cookie_status(chan, cookie, txstate);
  921. if (ret == DMA_COMPLETE)
  922. return ret;
  923. /*
  924. * There's no point calculating the residue if there's
  925. * no txstate to store the value.
  926. */
  927. if (!txstate)
  928. return DMA_ERROR;
  929. spin_lock_irqsave(&atchan->lock, flags);
  930. /* Get number of bytes left in the active transactions */
  931. bytes = atc_get_bytes_left(chan);
  932. spin_unlock_irqrestore(&atchan->lock, flags);
  933. if (unlikely(bytes < 0)) {
  934. dev_vdbg(chan2dev(chan), "get residual bytes error\n");
  935. return DMA_ERROR;
  936. } else {
  937. dma_set_residue(txstate, bytes);
  938. }
  939. dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
  940. ret, cookie, bytes);
  941. return ret;
  942. }
  943. /**
  944. * atc_issue_pending - try to finish work
  945. * @chan: target DMA channel
  946. */
  947. static void atc_issue_pending(struct dma_chan *chan)
  948. {
  949. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  950. unsigned long flags;
  951. dev_vdbg(chan2dev(chan), "issue_pending\n");
  952. /* Not needed for cyclic transfers */
  953. if (atc_chan_is_cyclic(atchan))
  954. return;
  955. spin_lock_irqsave(&atchan->lock, flags);
  956. atc_advance_work(atchan);
  957. spin_unlock_irqrestore(&atchan->lock, flags);
  958. }
  959. /**
  960. * atc_alloc_chan_resources - allocate resources for DMA channel
  961. * @chan: allocate descriptor resources for this channel
  962. * @client: current client requesting the channel be ready for requests
  963. *
  964. * return - the number of allocated descriptors
  965. */
  966. static int atc_alloc_chan_resources(struct dma_chan *chan)
  967. {
  968. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  969. struct at_dma *atdma = to_at_dma(chan->device);
  970. struct at_desc *desc;
  971. struct at_dma_slave *atslave;
  972. unsigned long flags;
  973. int i;
  974. u32 cfg;
  975. LIST_HEAD(tmp_list);
  976. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  977. /* ASSERT: channel is idle */
  978. if (atc_chan_is_enabled(atchan)) {
  979. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  980. return -EIO;
  981. }
  982. cfg = ATC_DEFAULT_CFG;
  983. atslave = chan->private;
  984. if (atslave) {
  985. /*
  986. * We need controller-specific data to set up slave
  987. * transfers.
  988. */
  989. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
  990. /* if cfg configuration specified take it instead of default */
  991. if (atslave->cfg)
  992. cfg = atslave->cfg;
  993. }
  994. /* have we already been set up?
  995. * reconfigure channel but no need to reallocate descriptors */
  996. if (!list_empty(&atchan->free_list))
  997. return atchan->descs_allocated;
  998. /* Allocate initial pool of descriptors */
  999. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1000. desc = atc_alloc_descriptor(chan, GFP_KERNEL);
  1001. if (!desc) {
  1002. dev_err(atdma->dma_common.dev,
  1003. "Only %d initial descriptors\n", i);
  1004. break;
  1005. }
  1006. list_add_tail(&desc->desc_node, &tmp_list);
  1007. }
  1008. spin_lock_irqsave(&atchan->lock, flags);
  1009. atchan->descs_allocated = i;
  1010. atchan->remain_desc = 0;
  1011. list_splice(&tmp_list, &atchan->free_list);
  1012. dma_cookie_init(chan);
  1013. spin_unlock_irqrestore(&atchan->lock, flags);
  1014. /* channel parameters */
  1015. channel_writel(atchan, CFG, cfg);
  1016. dev_dbg(chan2dev(chan),
  1017. "alloc_chan_resources: allocated %d descriptors\n",
  1018. atchan->descs_allocated);
  1019. return atchan->descs_allocated;
  1020. }
  1021. /**
  1022. * atc_free_chan_resources - free all channel resources
  1023. * @chan: DMA channel
  1024. */
  1025. static void atc_free_chan_resources(struct dma_chan *chan)
  1026. {
  1027. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1028. struct at_dma *atdma = to_at_dma(chan->device);
  1029. struct at_desc *desc, *_desc;
  1030. LIST_HEAD(list);
  1031. dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
  1032. atchan->descs_allocated);
  1033. /* ASSERT: channel is idle */
  1034. BUG_ON(!list_empty(&atchan->active_list));
  1035. BUG_ON(!list_empty(&atchan->queue));
  1036. BUG_ON(atc_chan_is_enabled(atchan));
  1037. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  1038. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  1039. list_del(&desc->desc_node);
  1040. /* free link descriptor */
  1041. dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
  1042. }
  1043. list_splice_init(&atchan->free_list, &list);
  1044. atchan->descs_allocated = 0;
  1045. atchan->status = 0;
  1046. atchan->remain_desc = 0;
  1047. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  1048. }
  1049. #ifdef CONFIG_OF
  1050. static bool at_dma_filter(struct dma_chan *chan, void *slave)
  1051. {
  1052. struct at_dma_slave *atslave = slave;
  1053. if (atslave->dma_dev == chan->device->dev) {
  1054. chan->private = atslave;
  1055. return true;
  1056. } else {
  1057. return false;
  1058. }
  1059. }
  1060. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1061. struct of_dma *of_dma)
  1062. {
  1063. struct dma_chan *chan;
  1064. struct at_dma_chan *atchan;
  1065. struct at_dma_slave *atslave;
  1066. dma_cap_mask_t mask;
  1067. unsigned int per_id;
  1068. struct platform_device *dmac_pdev;
  1069. if (dma_spec->args_count != 2)
  1070. return NULL;
  1071. dmac_pdev = of_find_device_by_node(dma_spec->np);
  1072. dma_cap_zero(mask);
  1073. dma_cap_set(DMA_SLAVE, mask);
  1074. atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
  1075. if (!atslave)
  1076. return NULL;
  1077. atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW;
  1078. /*
  1079. * We can fill both SRC_PER and DST_PER, one of these fields will be
  1080. * ignored depending on DMA transfer direction.
  1081. */
  1082. per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK;
  1083. atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
  1084. | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
  1085. /*
  1086. * We have to translate the value we get from the device tree since
  1087. * the half FIFO configuration value had to be 0 to keep backward
  1088. * compatibility.
  1089. */
  1090. switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) {
  1091. case AT91_DMA_CFG_FIFOCFG_ALAP:
  1092. atslave->cfg |= ATC_FIFOCFG_LARGESTBURST;
  1093. break;
  1094. case AT91_DMA_CFG_FIFOCFG_ASAP:
  1095. atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE;
  1096. break;
  1097. case AT91_DMA_CFG_FIFOCFG_HALF:
  1098. default:
  1099. atslave->cfg |= ATC_FIFOCFG_HALFFIFO;
  1100. }
  1101. atslave->dma_dev = &dmac_pdev->dev;
  1102. chan = dma_request_channel(mask, at_dma_filter, atslave);
  1103. if (!chan)
  1104. return NULL;
  1105. atchan = to_at_dma_chan(chan);
  1106. atchan->per_if = dma_spec->args[0] & 0xff;
  1107. atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
  1108. return chan;
  1109. }
  1110. #else
  1111. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1112. struct of_dma *of_dma)
  1113. {
  1114. return NULL;
  1115. }
  1116. #endif
  1117. /*-- Module Management -----------------------------------------------*/
  1118. /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
  1119. static struct at_dma_platform_data at91sam9rl_config = {
  1120. .nr_channels = 2,
  1121. };
  1122. static struct at_dma_platform_data at91sam9g45_config = {
  1123. .nr_channels = 8,
  1124. };
  1125. #if defined(CONFIG_OF)
  1126. static const struct of_device_id atmel_dma_dt_ids[] = {
  1127. {
  1128. .compatible = "atmel,at91sam9rl-dma",
  1129. .data = &at91sam9rl_config,
  1130. }, {
  1131. .compatible = "atmel,at91sam9g45-dma",
  1132. .data = &at91sam9g45_config,
  1133. }, {
  1134. /* sentinel */
  1135. }
  1136. };
  1137. MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
  1138. #endif
  1139. static const struct platform_device_id atdma_devtypes[] = {
  1140. {
  1141. .name = "at91sam9rl_dma",
  1142. .driver_data = (unsigned long) &at91sam9rl_config,
  1143. }, {
  1144. .name = "at91sam9g45_dma",
  1145. .driver_data = (unsigned long) &at91sam9g45_config,
  1146. }, {
  1147. /* sentinel */
  1148. }
  1149. };
  1150. static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
  1151. struct platform_device *pdev)
  1152. {
  1153. if (pdev->dev.of_node) {
  1154. const struct of_device_id *match;
  1155. match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
  1156. if (match == NULL)
  1157. return NULL;
  1158. return match->data;
  1159. }
  1160. return (struct at_dma_platform_data *)
  1161. platform_get_device_id(pdev)->driver_data;
  1162. }
  1163. /**
  1164. * at_dma_off - disable DMA controller
  1165. * @atdma: the Atmel HDAMC device
  1166. */
  1167. static void at_dma_off(struct at_dma *atdma)
  1168. {
  1169. dma_writel(atdma, EN, 0);
  1170. /* disable all interrupts */
  1171. dma_writel(atdma, EBCIDR, -1L);
  1172. /* confirm that all channels are disabled */
  1173. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  1174. cpu_relax();
  1175. }
  1176. static int __init at_dma_probe(struct platform_device *pdev)
  1177. {
  1178. struct resource *io;
  1179. struct at_dma *atdma;
  1180. size_t size;
  1181. int irq;
  1182. int err;
  1183. int i;
  1184. const struct at_dma_platform_data *plat_dat;
  1185. /* setup platform data for each SoC */
  1186. dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
  1187. dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
  1188. dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
  1189. /* get DMA parameters from controller type */
  1190. plat_dat = at_dma_get_driver_data(pdev);
  1191. if (!plat_dat)
  1192. return -ENODEV;
  1193. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1194. if (!io)
  1195. return -EINVAL;
  1196. irq = platform_get_irq(pdev, 0);
  1197. if (irq < 0)
  1198. return irq;
  1199. size = sizeof(struct at_dma);
  1200. size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
  1201. atdma = kzalloc(size, GFP_KERNEL);
  1202. if (!atdma)
  1203. return -ENOMEM;
  1204. /* discover transaction capabilities */
  1205. atdma->dma_common.cap_mask = plat_dat->cap_mask;
  1206. atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
  1207. size = resource_size(io);
  1208. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  1209. err = -EBUSY;
  1210. goto err_kfree;
  1211. }
  1212. atdma->regs = ioremap(io->start, size);
  1213. if (!atdma->regs) {
  1214. err = -ENOMEM;
  1215. goto err_release_r;
  1216. }
  1217. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  1218. if (IS_ERR(atdma->clk)) {
  1219. err = PTR_ERR(atdma->clk);
  1220. goto err_clk;
  1221. }
  1222. err = clk_prepare_enable(atdma->clk);
  1223. if (err)
  1224. goto err_clk_prepare;
  1225. /* force dma off, just in case */
  1226. at_dma_off(atdma);
  1227. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  1228. if (err)
  1229. goto err_irq;
  1230. platform_set_drvdata(pdev, atdma);
  1231. /* create a pool of consistent memory blocks for hardware descriptors */
  1232. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  1233. &pdev->dev, sizeof(struct at_desc),
  1234. 4 /* word alignment */, 0);
  1235. if (!atdma->dma_desc_pool) {
  1236. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1237. err = -ENOMEM;
  1238. goto err_pool_create;
  1239. }
  1240. /* clear any pending interrupt */
  1241. while (dma_readl(atdma, EBCISR))
  1242. cpu_relax();
  1243. /* initialize channels related values */
  1244. INIT_LIST_HEAD(&atdma->dma_common.channels);
  1245. for (i = 0; i < plat_dat->nr_channels; i++) {
  1246. struct at_dma_chan *atchan = &atdma->chan[i];
  1247. atchan->mem_if = AT_DMA_MEM_IF;
  1248. atchan->per_if = AT_DMA_PER_IF;
  1249. atchan->chan_common.device = &atdma->dma_common;
  1250. dma_cookie_init(&atchan->chan_common);
  1251. list_add_tail(&atchan->chan_common.device_node,
  1252. &atdma->dma_common.channels);
  1253. atchan->ch_regs = atdma->regs + ch_regs(i);
  1254. spin_lock_init(&atchan->lock);
  1255. atchan->mask = 1 << i;
  1256. INIT_LIST_HEAD(&atchan->active_list);
  1257. INIT_LIST_HEAD(&atchan->queue);
  1258. INIT_LIST_HEAD(&atchan->free_list);
  1259. tasklet_init(&atchan->tasklet, atc_tasklet,
  1260. (unsigned long)atchan);
  1261. atc_enable_chan_irq(atdma, i);
  1262. }
  1263. /* set base routines */
  1264. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  1265. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  1266. atdma->dma_common.device_tx_status = atc_tx_status;
  1267. atdma->dma_common.device_issue_pending = atc_issue_pending;
  1268. atdma->dma_common.dev = &pdev->dev;
  1269. /* set prep routines based on capability */
  1270. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  1271. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  1272. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  1273. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  1274. /* controller can do slave DMA: can trigger cyclic transfers */
  1275. dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
  1276. atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
  1277. atdma->dma_common.device_control = atc_control;
  1278. }
  1279. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1280. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
  1281. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  1282. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  1283. plat_dat->nr_channels);
  1284. dma_async_device_register(&atdma->dma_common);
  1285. /*
  1286. * Do not return an error if the dmac node is not present in order to
  1287. * not break the existing way of requesting channel with
  1288. * dma_request_channel().
  1289. */
  1290. if (pdev->dev.of_node) {
  1291. err = of_dma_controller_register(pdev->dev.of_node,
  1292. at_dma_xlate, atdma);
  1293. if (err) {
  1294. dev_err(&pdev->dev, "could not register of_dma_controller\n");
  1295. goto err_of_dma_controller_register;
  1296. }
  1297. }
  1298. return 0;
  1299. err_of_dma_controller_register:
  1300. dma_async_device_unregister(&atdma->dma_common);
  1301. dma_pool_destroy(atdma->dma_desc_pool);
  1302. err_pool_create:
  1303. free_irq(platform_get_irq(pdev, 0), atdma);
  1304. err_irq:
  1305. clk_disable_unprepare(atdma->clk);
  1306. err_clk_prepare:
  1307. clk_put(atdma->clk);
  1308. err_clk:
  1309. iounmap(atdma->regs);
  1310. atdma->regs = NULL;
  1311. err_release_r:
  1312. release_mem_region(io->start, size);
  1313. err_kfree:
  1314. kfree(atdma);
  1315. return err;
  1316. }
  1317. static int at_dma_remove(struct platform_device *pdev)
  1318. {
  1319. struct at_dma *atdma = platform_get_drvdata(pdev);
  1320. struct dma_chan *chan, *_chan;
  1321. struct resource *io;
  1322. at_dma_off(atdma);
  1323. dma_async_device_unregister(&atdma->dma_common);
  1324. dma_pool_destroy(atdma->dma_desc_pool);
  1325. free_irq(platform_get_irq(pdev, 0), atdma);
  1326. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1327. device_node) {
  1328. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1329. /* Disable interrupts */
  1330. atc_disable_chan_irq(atdma, chan->chan_id);
  1331. tasklet_disable(&atchan->tasklet);
  1332. tasklet_kill(&atchan->tasklet);
  1333. list_del(&chan->device_node);
  1334. }
  1335. clk_disable_unprepare(atdma->clk);
  1336. clk_put(atdma->clk);
  1337. iounmap(atdma->regs);
  1338. atdma->regs = NULL;
  1339. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1340. release_mem_region(io->start, resource_size(io));
  1341. kfree(atdma);
  1342. return 0;
  1343. }
  1344. static void at_dma_shutdown(struct platform_device *pdev)
  1345. {
  1346. struct at_dma *atdma = platform_get_drvdata(pdev);
  1347. at_dma_off(platform_get_drvdata(pdev));
  1348. clk_disable_unprepare(atdma->clk);
  1349. }
  1350. static int at_dma_prepare(struct device *dev)
  1351. {
  1352. struct platform_device *pdev = to_platform_device(dev);
  1353. struct at_dma *atdma = platform_get_drvdata(pdev);
  1354. struct dma_chan *chan, *_chan;
  1355. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1356. device_node) {
  1357. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1358. /* wait for transaction completion (except in cyclic case) */
  1359. if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
  1360. return -EAGAIN;
  1361. }
  1362. return 0;
  1363. }
  1364. static void atc_suspend_cyclic(struct at_dma_chan *atchan)
  1365. {
  1366. struct dma_chan *chan = &atchan->chan_common;
  1367. /* Channel should be paused by user
  1368. * do it anyway even if it is not done already */
  1369. if (!atc_chan_is_paused(atchan)) {
  1370. dev_warn(chan2dev(chan),
  1371. "cyclic channel not paused, should be done by channel user\n");
  1372. atc_control(chan, DMA_PAUSE, 0);
  1373. }
  1374. /* now preserve additional data for cyclic operations */
  1375. /* next descriptor address in the cyclic list */
  1376. atchan->save_dscr = channel_readl(atchan, DSCR);
  1377. vdbg_dump_regs(atchan);
  1378. }
  1379. static int at_dma_suspend_noirq(struct device *dev)
  1380. {
  1381. struct platform_device *pdev = to_platform_device(dev);
  1382. struct at_dma *atdma = platform_get_drvdata(pdev);
  1383. struct dma_chan *chan, *_chan;
  1384. /* preserve data */
  1385. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1386. device_node) {
  1387. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1388. if (atc_chan_is_cyclic(atchan))
  1389. atc_suspend_cyclic(atchan);
  1390. atchan->save_cfg = channel_readl(atchan, CFG);
  1391. }
  1392. atdma->save_imr = dma_readl(atdma, EBCIMR);
  1393. /* disable DMA controller */
  1394. at_dma_off(atdma);
  1395. clk_disable_unprepare(atdma->clk);
  1396. return 0;
  1397. }
  1398. static void atc_resume_cyclic(struct at_dma_chan *atchan)
  1399. {
  1400. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  1401. /* restore channel status for cyclic descriptors list:
  1402. * next descriptor in the cyclic list at the time of suspend */
  1403. channel_writel(atchan, SADDR, 0);
  1404. channel_writel(atchan, DADDR, 0);
  1405. channel_writel(atchan, CTRLA, 0);
  1406. channel_writel(atchan, CTRLB, 0);
  1407. channel_writel(atchan, DSCR, atchan->save_dscr);
  1408. dma_writel(atdma, CHER, atchan->mask);
  1409. /* channel pause status should be removed by channel user
  1410. * We cannot take the initiative to do it here */
  1411. vdbg_dump_regs(atchan);
  1412. }
  1413. static int at_dma_resume_noirq(struct device *dev)
  1414. {
  1415. struct platform_device *pdev = to_platform_device(dev);
  1416. struct at_dma *atdma = platform_get_drvdata(pdev);
  1417. struct dma_chan *chan, *_chan;
  1418. /* bring back DMA controller */
  1419. clk_prepare_enable(atdma->clk);
  1420. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1421. /* clear any pending interrupt */
  1422. while (dma_readl(atdma, EBCISR))
  1423. cpu_relax();
  1424. /* restore saved data */
  1425. dma_writel(atdma, EBCIER, atdma->save_imr);
  1426. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1427. device_node) {
  1428. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1429. channel_writel(atchan, CFG, atchan->save_cfg);
  1430. if (atc_chan_is_cyclic(atchan))
  1431. atc_resume_cyclic(atchan);
  1432. }
  1433. return 0;
  1434. }
  1435. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  1436. .prepare = at_dma_prepare,
  1437. .suspend_noirq = at_dma_suspend_noirq,
  1438. .resume_noirq = at_dma_resume_noirq,
  1439. };
  1440. static struct platform_driver at_dma_driver = {
  1441. .remove = at_dma_remove,
  1442. .shutdown = at_dma_shutdown,
  1443. .id_table = atdma_devtypes,
  1444. .driver = {
  1445. .name = "at_hdmac",
  1446. .pm = &at_dma_dev_pm_ops,
  1447. .of_match_table = of_match_ptr(atmel_dma_dt_ids),
  1448. },
  1449. };
  1450. static int __init at_dma_init(void)
  1451. {
  1452. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1453. }
  1454. subsys_initcall(at_dma_init);
  1455. static void __exit at_dma_exit(void)
  1456. {
  1457. platform_driver_unregister(&at_dma_driver);
  1458. }
  1459. module_exit(at_dma_exit);
  1460. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1461. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1462. MODULE_LICENSE("GPL");
  1463. MODULE_ALIAS("platform:at_hdmac");