at_hdmac.c 47 KB

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