at_hdmac.c 44 KB

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