at_hdmac.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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,
  13. *
  14. * The driver has currently been tested with the Atmel AT91SAM9RL
  15. * and AT91SAM9G45 series.
  16. */
  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 "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_CTRLA (0)
  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. /*----------------------------------------------------------------------*/
  52. static struct at_desc *atc_first_active(struct at_dma_chan *atchan)
  53. {
  54. return list_first_entry(&atchan->active_list,
  55. struct at_desc, desc_node);
  56. }
  57. static struct at_desc *atc_first_queued(struct at_dma_chan *atchan)
  58. {
  59. return list_first_entry(&atchan->queue,
  60. struct at_desc, desc_node);
  61. }
  62. /**
  63. * atc_alloc_descriptor - allocate and return an initialized descriptor
  64. * @chan: the channel to allocate descriptors for
  65. * @gfp_flags: GFP allocation flags
  66. *
  67. * Note: The ack-bit is positioned in the descriptor flag at creation time
  68. * to make initial allocation more convenient. This bit will be cleared
  69. * and control will be given to client at usage time (during
  70. * preparation functions).
  71. */
  72. static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan,
  73. gfp_t gfp_flags)
  74. {
  75. struct at_desc *desc = NULL;
  76. struct at_dma *atdma = to_at_dma(chan->device);
  77. dma_addr_t phys;
  78. desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys);
  79. if (desc) {
  80. memset(desc, 0, sizeof(struct at_desc));
  81. INIT_LIST_HEAD(&desc->tx_list);
  82. dma_async_tx_descriptor_init(&desc->txd, chan);
  83. /* txd.flags will be overwritten in prep functions */
  84. desc->txd.flags = DMA_CTRL_ACK;
  85. desc->txd.tx_submit = atc_tx_submit;
  86. desc->txd.phys = phys;
  87. }
  88. return desc;
  89. }
  90. /**
  91. * atc_desc_get - get an unused descriptor from free_list
  92. * @atchan: channel we want a new descriptor for
  93. */
  94. static struct at_desc *atc_desc_get(struct at_dma_chan *atchan)
  95. {
  96. struct at_desc *desc, *_desc;
  97. struct at_desc *ret = NULL;
  98. unsigned long flags;
  99. unsigned int i = 0;
  100. LIST_HEAD(tmp_list);
  101. spin_lock_irqsave(&atchan->lock, flags);
  102. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  103. i++;
  104. if (async_tx_test_ack(&desc->txd)) {
  105. list_del(&desc->desc_node);
  106. ret = desc;
  107. break;
  108. }
  109. dev_dbg(chan2dev(&atchan->chan_common),
  110. "desc %p not ACKed\n", desc);
  111. }
  112. spin_unlock_irqrestore(&atchan->lock, flags);
  113. dev_vdbg(chan2dev(&atchan->chan_common),
  114. "scanned %u descriptors on freelist\n", i);
  115. /* no more descriptor available in initial pool: create one more */
  116. if (!ret) {
  117. ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC);
  118. if (ret) {
  119. spin_lock_irqsave(&atchan->lock, flags);
  120. atchan->descs_allocated++;
  121. spin_unlock_irqrestore(&atchan->lock, flags);
  122. } else {
  123. dev_err(chan2dev(&atchan->chan_common),
  124. "not enough descriptors available\n");
  125. }
  126. }
  127. return ret;
  128. }
  129. /**
  130. * atc_desc_put - move a descriptor, including any children, to the free list
  131. * @atchan: channel we work on
  132. * @desc: descriptor, at the head of a chain, to move to free list
  133. */
  134. static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc)
  135. {
  136. if (desc) {
  137. struct at_desc *child;
  138. unsigned long flags;
  139. spin_lock_irqsave(&atchan->lock, flags);
  140. list_for_each_entry(child, &desc->tx_list, desc_node)
  141. dev_vdbg(chan2dev(&atchan->chan_common),
  142. "moving child desc %p to freelist\n",
  143. child);
  144. list_splice_init(&desc->tx_list, &atchan->free_list);
  145. dev_vdbg(chan2dev(&atchan->chan_common),
  146. "moving desc %p to freelist\n", desc);
  147. list_add(&desc->desc_node, &atchan->free_list);
  148. spin_unlock_irqrestore(&atchan->lock, flags);
  149. }
  150. }
  151. /**
  152. * atc_desc_chain - build chain adding a descripor
  153. * @first: address of first descripor of the chain
  154. * @prev: address of previous descripor of the chain
  155. * @desc: descriptor to queue
  156. *
  157. * Called from prep_* functions
  158. */
  159. static void atc_desc_chain(struct at_desc **first, struct at_desc **prev,
  160. struct at_desc *desc)
  161. {
  162. if (!(*first)) {
  163. *first = desc;
  164. } else {
  165. /* inform the HW lli about chaining */
  166. (*prev)->lli.dscr = desc->txd.phys;
  167. /* insert the link descriptor to the LD ring */
  168. list_add_tail(&desc->desc_node,
  169. &(*first)->tx_list);
  170. }
  171. *prev = desc;
  172. }
  173. /**
  174. * atc_dostart - starts the DMA engine for real
  175. * @atchan: the channel we want to start
  176. * @first: first descriptor in the list we want to begin with
  177. *
  178. * Called with atchan->lock held and bh disabled
  179. */
  180. static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
  181. {
  182. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  183. /* ASSERT: channel is idle */
  184. if (atc_chan_is_enabled(atchan)) {
  185. dev_err(chan2dev(&atchan->chan_common),
  186. "BUG: Attempted to start non-idle channel\n");
  187. dev_err(chan2dev(&atchan->chan_common),
  188. " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
  189. channel_readl(atchan, SADDR),
  190. channel_readl(atchan, DADDR),
  191. channel_readl(atchan, CTRLA),
  192. channel_readl(atchan, CTRLB),
  193. channel_readl(atchan, DSCR));
  194. /* The tasklet will hopefully advance the queue... */
  195. return;
  196. }
  197. vdbg_dump_regs(atchan);
  198. channel_writel(atchan, SADDR, 0);
  199. channel_writel(atchan, DADDR, 0);
  200. channel_writel(atchan, CTRLA, 0);
  201. channel_writel(atchan, CTRLB, 0);
  202. channel_writel(atchan, DSCR, first->txd.phys);
  203. dma_writel(atdma, CHER, atchan->mask);
  204. vdbg_dump_regs(atchan);
  205. }
  206. /**
  207. * atc_chain_complete - finish work for one transaction chain
  208. * @atchan: channel we work on
  209. * @desc: descriptor at the head of the chain we want do complete
  210. *
  211. * Called with atchan->lock held and bh disabled */
  212. static void
  213. atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
  214. {
  215. struct dma_async_tx_descriptor *txd = &desc->txd;
  216. dev_vdbg(chan2dev(&atchan->chan_common),
  217. "descriptor %u complete\n", txd->cookie);
  218. /* mark the descriptor as complete for non cyclic cases only */
  219. if (!atc_chan_is_cyclic(atchan))
  220. dma_cookie_complete(txd);
  221. /* move children to free_list */
  222. list_splice_init(&desc->tx_list, &atchan->free_list);
  223. /* move myself to free_list */
  224. list_move(&desc->desc_node, &atchan->free_list);
  225. /* unmap dma addresses (not on slave channels) */
  226. if (!atchan->chan_common.private) {
  227. struct device *parent = chan2parent(&atchan->chan_common);
  228. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  229. if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  230. dma_unmap_single(parent,
  231. desc->lli.daddr,
  232. desc->len, DMA_FROM_DEVICE);
  233. else
  234. dma_unmap_page(parent,
  235. desc->lli.daddr,
  236. desc->len, DMA_FROM_DEVICE);
  237. }
  238. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  239. if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  240. dma_unmap_single(parent,
  241. desc->lli.saddr,
  242. desc->len, DMA_TO_DEVICE);
  243. else
  244. dma_unmap_page(parent,
  245. desc->lli.saddr,
  246. desc->len, DMA_TO_DEVICE);
  247. }
  248. }
  249. /* for cyclic transfers,
  250. * no need to replay callback function while stopping */
  251. if (!atc_chan_is_cyclic(atchan)) {
  252. dma_async_tx_callback callback = txd->callback;
  253. void *param = txd->callback_param;
  254. /*
  255. * The API requires that no submissions are done from a
  256. * callback, so we don't need to drop the lock here
  257. */
  258. if (callback)
  259. callback(param);
  260. }
  261. dma_run_dependencies(txd);
  262. }
  263. /**
  264. * atc_complete_all - finish work for all transactions
  265. * @atchan: channel to complete transactions for
  266. *
  267. * Eventually submit queued descriptors if any
  268. *
  269. * Assume channel is idle while calling this function
  270. * Called with atchan->lock held and bh disabled
  271. */
  272. static void atc_complete_all(struct at_dma_chan *atchan)
  273. {
  274. struct at_desc *desc, *_desc;
  275. LIST_HEAD(list);
  276. dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
  277. BUG_ON(atc_chan_is_enabled(atchan));
  278. /*
  279. * Submit queued descriptors ASAP, i.e. before we go through
  280. * the completed ones.
  281. */
  282. if (!list_empty(&atchan->queue))
  283. atc_dostart(atchan, atc_first_queued(atchan));
  284. /* empty active_list now it is completed */
  285. list_splice_init(&atchan->active_list, &list);
  286. /* empty queue list by moving descriptors (if any) to active_list */
  287. list_splice_init(&atchan->queue, &atchan->active_list);
  288. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  289. atc_chain_complete(atchan, desc);
  290. }
  291. /**
  292. * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
  293. * @atchan: channel to be cleaned up
  294. *
  295. * Called with atchan->lock held and bh disabled
  296. */
  297. static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
  298. {
  299. struct at_desc *desc, *_desc;
  300. struct at_desc *child;
  301. dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
  302. list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
  303. if (!(desc->lli.ctrla & ATC_DONE))
  304. /* This one is currently in progress */
  305. return;
  306. list_for_each_entry(child, &desc->tx_list, desc_node)
  307. if (!(child->lli.ctrla & ATC_DONE))
  308. /* Currently in progress */
  309. return;
  310. /*
  311. * No descriptors so far seem to be in progress, i.e.
  312. * this chain must be done.
  313. */
  314. atc_chain_complete(atchan, desc);
  315. }
  316. }
  317. /**
  318. * atc_advance_work - at the end of a transaction, move forward
  319. * @atchan: channel where the transaction ended
  320. *
  321. * Called with atchan->lock held and bh disabled
  322. */
  323. static void atc_advance_work(struct at_dma_chan *atchan)
  324. {
  325. dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
  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. ctrla = ATC_DEFAULT_CTRLA;
  499. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  500. | ATC_SRC_ADDR_MODE_INCR
  501. | ATC_DST_ADDR_MODE_INCR
  502. | ATC_FC_MEM2MEM;
  503. /*
  504. * We can be a lot more clever here, but this should take care
  505. * of the most common optimization.
  506. */
  507. if (!((src | dest | len) & 3)) {
  508. ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
  509. src_width = dst_width = 2;
  510. } else if (!((src | dest | len) & 1)) {
  511. ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
  512. src_width = dst_width = 1;
  513. } else {
  514. ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
  515. src_width = dst_width = 0;
  516. }
  517. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  518. xfer_count = min_t(size_t, (len - offset) >> src_width,
  519. ATC_BTSIZE_MAX);
  520. desc = atc_desc_get(atchan);
  521. if (!desc)
  522. goto err_desc_get;
  523. desc->lli.saddr = src + offset;
  524. desc->lli.daddr = dest + offset;
  525. desc->lli.ctrla = ctrla | xfer_count;
  526. desc->lli.ctrlb = ctrlb;
  527. desc->txd.cookie = 0;
  528. atc_desc_chain(&first, &prev, desc);
  529. }
  530. /* First descriptor of the chain embedds additional information */
  531. first->txd.cookie = -EBUSY;
  532. first->len = len;
  533. /* set end-of-link to the last link descriptor of list*/
  534. set_desc_eol(desc);
  535. first->txd.flags = flags; /* client is in control of this ack */
  536. return &first->txd;
  537. err_desc_get:
  538. atc_desc_put(atchan, first);
  539. return NULL;
  540. }
  541. /**
  542. * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  543. * @chan: DMA channel
  544. * @sgl: scatterlist to transfer to/from
  545. * @sg_len: number of entries in @scatterlist
  546. * @direction: DMA direction
  547. * @flags: tx descriptor status flags
  548. * @context: transaction context (ignored)
  549. */
  550. static struct dma_async_tx_descriptor *
  551. atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  552. unsigned int sg_len, enum dma_transfer_direction direction,
  553. unsigned long flags, void *context)
  554. {
  555. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  556. struct at_dma_slave *atslave = chan->private;
  557. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  558. struct at_desc *first = NULL;
  559. struct at_desc *prev = NULL;
  560. u32 ctrla;
  561. u32 ctrlb;
  562. dma_addr_t reg;
  563. unsigned int reg_width;
  564. unsigned int mem_width;
  565. unsigned int i;
  566. struct scatterlist *sg;
  567. size_t total_len = 0;
  568. dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
  569. sg_len,
  570. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  571. flags);
  572. if (unlikely(!atslave || !sg_len)) {
  573. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  574. return NULL;
  575. }
  576. ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
  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(AT_DMA_MEM_IF) | ATC_DIF(AT_DMA_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. mem_width = 2;
  597. if (unlikely(mem & 3 || len & 3))
  598. mem_width = 0;
  599. desc->lli.saddr = mem;
  600. desc->lli.daddr = reg;
  601. desc->lli.ctrla = ctrla
  602. | ATC_SRC_WIDTH(mem_width)
  603. | len >> mem_width;
  604. desc->lli.ctrlb = ctrlb;
  605. atc_desc_chain(&first, &prev, desc);
  606. total_len += len;
  607. }
  608. break;
  609. case DMA_DEV_TO_MEM:
  610. reg_width = convert_buswidth(sconfig->src_addr_width);
  611. ctrla |= ATC_SRC_WIDTH(reg_width);
  612. ctrlb |= ATC_DST_ADDR_MODE_INCR
  613. | ATC_SRC_ADDR_MODE_FIXED
  614. | ATC_FC_PER2MEM
  615. | ATC_SIF(AT_DMA_PER_IF) | ATC_DIF(AT_DMA_MEM_IF);
  616. reg = sconfig->src_addr;
  617. for_each_sg(sgl, sg, sg_len, i) {
  618. struct at_desc *desc;
  619. u32 len;
  620. u32 mem;
  621. desc = atc_desc_get(atchan);
  622. if (!desc)
  623. goto err_desc_get;
  624. mem = sg_dma_address(sg);
  625. len = sg_dma_len(sg);
  626. mem_width = 2;
  627. if (unlikely(mem & 3 || len & 3))
  628. mem_width = 0;
  629. desc->lli.saddr = reg;
  630. desc->lli.daddr = mem;
  631. desc->lli.ctrla = ctrla
  632. | ATC_DST_WIDTH(mem_width)
  633. | len >> reg_width;
  634. desc->lli.ctrlb = ctrlb;
  635. atc_desc_chain(&first, &prev, desc);
  636. total_len += len;
  637. }
  638. break;
  639. default:
  640. return NULL;
  641. }
  642. /* set end-of-link to the last link descriptor of list*/
  643. set_desc_eol(prev);
  644. /* First descriptor of the chain embedds additional information */
  645. first->txd.cookie = -EBUSY;
  646. first->len = total_len;
  647. /* first link descriptor of list is responsible of flags */
  648. first->txd.flags = flags; /* client is in control of this ack */
  649. return &first->txd;
  650. err_desc_get:
  651. dev_err(chan2dev(chan), "not enough descriptors available\n");
  652. atc_desc_put(atchan, first);
  653. return NULL;
  654. }
  655. /**
  656. * atc_dma_cyclic_check_values
  657. * Check for too big/unaligned periods and unaligned DMA buffer
  658. */
  659. static int
  660. atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
  661. size_t period_len, enum dma_transfer_direction direction)
  662. {
  663. if (period_len > (ATC_BTSIZE_MAX << reg_width))
  664. goto err_out;
  665. if (unlikely(period_len & ((1 << reg_width) - 1)))
  666. goto err_out;
  667. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  668. goto err_out;
  669. if (unlikely(!(direction & (DMA_DEV_TO_MEM | DMA_MEM_TO_DEV))))
  670. goto err_out;
  671. return 0;
  672. err_out:
  673. return -EINVAL;
  674. }
  675. /**
  676. * atc_dma_cyclic_fill_desc - Fill one period decriptor
  677. */
  678. static int
  679. atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
  680. unsigned int period_index, dma_addr_t buf_addr,
  681. unsigned int reg_width, size_t period_len,
  682. enum dma_transfer_direction direction)
  683. {
  684. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  685. struct at_dma_slave *atslave = chan->private;
  686. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  687. u32 ctrla;
  688. /* prepare common CRTLA value */
  689. ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla
  690. | ATC_DST_WIDTH(reg_width)
  691. | ATC_SRC_WIDTH(reg_width)
  692. | period_len >> reg_width;
  693. switch (direction) {
  694. case DMA_MEM_TO_DEV:
  695. desc->lli.saddr = buf_addr + (period_len * period_index);
  696. desc->lli.daddr = sconfig->dst_addr;
  697. desc->lli.ctrla = ctrla;
  698. desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
  699. | ATC_SRC_ADDR_MODE_INCR
  700. | ATC_FC_MEM2PER
  701. | ATC_SIF(AT_DMA_MEM_IF)
  702. | ATC_DIF(AT_DMA_PER_IF);
  703. break;
  704. case DMA_DEV_TO_MEM:
  705. desc->lli.saddr = sconfig->src_addr;
  706. desc->lli.daddr = buf_addr + (period_len * period_index);
  707. desc->lli.ctrla = ctrla;
  708. desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
  709. | ATC_SRC_ADDR_MODE_FIXED
  710. | ATC_FC_PER2MEM
  711. | ATC_SIF(AT_DMA_PER_IF)
  712. | ATC_DIF(AT_DMA_MEM_IF);
  713. break;
  714. default:
  715. return -EINVAL;
  716. }
  717. return 0;
  718. }
  719. /**
  720. * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
  721. * @chan: the DMA channel to prepare
  722. * @buf_addr: physical DMA address where the buffer starts
  723. * @buf_len: total number of bytes for the entire buffer
  724. * @period_len: number of bytes for each period
  725. * @direction: transfer direction, to or from device
  726. * @context: transfer context (ignored)
  727. */
  728. static struct dma_async_tx_descriptor *
  729. atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  730. size_t period_len, enum dma_transfer_direction direction,
  731. void *context)
  732. {
  733. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  734. struct at_dma_slave *atslave = chan->private;
  735. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  736. struct at_desc *first = NULL;
  737. struct at_desc *prev = NULL;
  738. unsigned long was_cyclic;
  739. unsigned int reg_width;
  740. unsigned int periods = buf_len / period_len;
  741. unsigned int i;
  742. dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
  743. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  744. buf_addr,
  745. periods, buf_len, period_len);
  746. if (unlikely(!atslave || !buf_len || !period_len)) {
  747. dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
  748. return NULL;
  749. }
  750. was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
  751. if (was_cyclic) {
  752. dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
  753. return NULL;
  754. }
  755. if (sconfig->direction == DMA_MEM_TO_DEV)
  756. reg_width = convert_buswidth(sconfig->dst_addr_width);
  757. else
  758. reg_width = convert_buswidth(sconfig->src_addr_width);
  759. /* Check for too big/unaligned periods and unaligned DMA buffer */
  760. if (atc_dma_cyclic_check_values(reg_width, buf_addr,
  761. period_len, direction))
  762. goto err_out;
  763. /* build cyclic linked list */
  764. for (i = 0; i < periods; i++) {
  765. struct at_desc *desc;
  766. desc = atc_desc_get(atchan);
  767. if (!desc)
  768. goto err_desc_get;
  769. if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
  770. reg_width, period_len, direction))
  771. goto err_desc_get;
  772. atc_desc_chain(&first, &prev, desc);
  773. }
  774. /* lets make a cyclic list */
  775. prev->lli.dscr = first->txd.phys;
  776. /* First descriptor of the chain embedds additional information */
  777. first->txd.cookie = -EBUSY;
  778. first->len = buf_len;
  779. return &first->txd;
  780. err_desc_get:
  781. dev_err(chan2dev(chan), "not enough descriptors available\n");
  782. atc_desc_put(atchan, first);
  783. err_out:
  784. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  785. return NULL;
  786. }
  787. static int set_runtime_config(struct dma_chan *chan,
  788. struct dma_slave_config *sconfig)
  789. {
  790. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  791. /* Check if it is chan is configured for slave transfers */
  792. if (!chan->private)
  793. return -EINVAL;
  794. memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
  795. convert_burst(&atchan->dma_sconfig.src_maxburst);
  796. convert_burst(&atchan->dma_sconfig.dst_maxburst);
  797. return 0;
  798. }
  799. static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  800. unsigned long arg)
  801. {
  802. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  803. struct at_dma *atdma = to_at_dma(chan->device);
  804. int chan_id = atchan->chan_common.chan_id;
  805. unsigned long flags;
  806. LIST_HEAD(list);
  807. dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
  808. if (cmd == DMA_PAUSE) {
  809. spin_lock_irqsave(&atchan->lock, flags);
  810. dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
  811. set_bit(ATC_IS_PAUSED, &atchan->status);
  812. spin_unlock_irqrestore(&atchan->lock, flags);
  813. } else if (cmd == DMA_RESUME) {
  814. if (!atc_chan_is_paused(atchan))
  815. return 0;
  816. spin_lock_irqsave(&atchan->lock, flags);
  817. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
  818. clear_bit(ATC_IS_PAUSED, &atchan->status);
  819. spin_unlock_irqrestore(&atchan->lock, flags);
  820. } else if (cmd == DMA_TERMINATE_ALL) {
  821. struct at_desc *desc, *_desc;
  822. /*
  823. * This is only called when something went wrong elsewhere, so
  824. * we don't really care about the data. Just disable the
  825. * channel. We still have to poll the channel enable bit due
  826. * to AHB/HSB limitations.
  827. */
  828. spin_lock_irqsave(&atchan->lock, flags);
  829. /* disabling channel: must also remove suspend state */
  830. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
  831. /* confirm that this channel is disabled */
  832. while (dma_readl(atdma, CHSR) & atchan->mask)
  833. cpu_relax();
  834. /* active_list entries will end up before queued entries */
  835. list_splice_init(&atchan->queue, &list);
  836. list_splice_init(&atchan->active_list, &list);
  837. /* Flush all pending and queued descriptors */
  838. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  839. atc_chain_complete(atchan, desc);
  840. clear_bit(ATC_IS_PAUSED, &atchan->status);
  841. /* if channel dedicated to cyclic operations, free it */
  842. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  843. spin_unlock_irqrestore(&atchan->lock, flags);
  844. } else if (cmd == DMA_SLAVE_CONFIG) {
  845. return set_runtime_config(chan, (struct dma_slave_config *)arg);
  846. } else {
  847. return -ENXIO;
  848. }
  849. return 0;
  850. }
  851. /**
  852. * atc_tx_status - poll for transaction completion
  853. * @chan: DMA channel
  854. * @cookie: transaction identifier to check status of
  855. * @txstate: if not %NULL updated with transaction state
  856. *
  857. * If @txstate is passed in, upon return it reflect the driver
  858. * internal state and can be used with dma_async_is_complete() to check
  859. * the status of multiple cookies without re-checking hardware state.
  860. */
  861. static enum dma_status
  862. atc_tx_status(struct dma_chan *chan,
  863. dma_cookie_t cookie,
  864. struct dma_tx_state *txstate)
  865. {
  866. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  867. dma_cookie_t last_used;
  868. dma_cookie_t last_complete;
  869. unsigned long flags;
  870. enum dma_status ret;
  871. spin_lock_irqsave(&atchan->lock, flags);
  872. ret = dma_cookie_status(chan, cookie, txstate);
  873. if (ret != DMA_SUCCESS) {
  874. atc_cleanup_descriptors(atchan);
  875. ret = dma_cookie_status(chan, cookie, txstate);
  876. }
  877. last_complete = chan->completed_cookie;
  878. last_used = chan->cookie;
  879. spin_unlock_irqrestore(&atchan->lock, flags);
  880. if (ret != DMA_SUCCESS)
  881. dma_set_residue(txstate, atc_first_active(atchan)->len);
  882. if (atc_chan_is_paused(atchan))
  883. ret = DMA_PAUSED;
  884. dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d (d%d, u%d)\n",
  885. ret, cookie, last_complete ? last_complete : 0,
  886. last_used ? last_used : 0);
  887. return ret;
  888. }
  889. /**
  890. * atc_issue_pending - try to finish work
  891. * @chan: target DMA channel
  892. */
  893. static void atc_issue_pending(struct dma_chan *chan)
  894. {
  895. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  896. unsigned long flags;
  897. dev_vdbg(chan2dev(chan), "issue_pending\n");
  898. /* Not needed for cyclic transfers */
  899. if (atc_chan_is_cyclic(atchan))
  900. return;
  901. spin_lock_irqsave(&atchan->lock, flags);
  902. if (!atc_chan_is_enabled(atchan)) {
  903. atc_advance_work(atchan);
  904. }
  905. spin_unlock_irqrestore(&atchan->lock, flags);
  906. }
  907. /**
  908. * atc_alloc_chan_resources - allocate resources for DMA channel
  909. * @chan: allocate descriptor resources for this channel
  910. * @client: current client requesting the channel be ready for requests
  911. *
  912. * return - the number of allocated descriptors
  913. */
  914. static int atc_alloc_chan_resources(struct dma_chan *chan)
  915. {
  916. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  917. struct at_dma *atdma = to_at_dma(chan->device);
  918. struct at_desc *desc;
  919. struct at_dma_slave *atslave;
  920. unsigned long flags;
  921. int i;
  922. u32 cfg;
  923. LIST_HEAD(tmp_list);
  924. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  925. /* ASSERT: channel is idle */
  926. if (atc_chan_is_enabled(atchan)) {
  927. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  928. return -EIO;
  929. }
  930. cfg = ATC_DEFAULT_CFG;
  931. atslave = chan->private;
  932. if (atslave) {
  933. /*
  934. * We need controller-specific data to set up slave
  935. * transfers.
  936. */
  937. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
  938. /* if cfg configuration specified take it instad of default */
  939. if (atslave->cfg)
  940. cfg = atslave->cfg;
  941. }
  942. /* have we already been set up?
  943. * reconfigure channel but no need to reallocate descriptors */
  944. if (!list_empty(&atchan->free_list))
  945. return atchan->descs_allocated;
  946. /* Allocate initial pool of descriptors */
  947. for (i = 0; i < init_nr_desc_per_channel; i++) {
  948. desc = atc_alloc_descriptor(chan, GFP_KERNEL);
  949. if (!desc) {
  950. dev_err(atdma->dma_common.dev,
  951. "Only %d initial descriptors\n", i);
  952. break;
  953. }
  954. list_add_tail(&desc->desc_node, &tmp_list);
  955. }
  956. spin_lock_irqsave(&atchan->lock, flags);
  957. atchan->descs_allocated = i;
  958. list_splice(&tmp_list, &atchan->free_list);
  959. dma_cookie_init(chan);
  960. spin_unlock_irqrestore(&atchan->lock, flags);
  961. /* channel parameters */
  962. channel_writel(atchan, CFG, cfg);
  963. dev_dbg(chan2dev(chan),
  964. "alloc_chan_resources: allocated %d descriptors\n",
  965. atchan->descs_allocated);
  966. return atchan->descs_allocated;
  967. }
  968. /**
  969. * atc_free_chan_resources - free all channel resources
  970. * @chan: DMA channel
  971. */
  972. static void atc_free_chan_resources(struct dma_chan *chan)
  973. {
  974. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  975. struct at_dma *atdma = to_at_dma(chan->device);
  976. struct at_desc *desc, *_desc;
  977. LIST_HEAD(list);
  978. dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
  979. atchan->descs_allocated);
  980. /* ASSERT: channel is idle */
  981. BUG_ON(!list_empty(&atchan->active_list));
  982. BUG_ON(!list_empty(&atchan->queue));
  983. BUG_ON(atc_chan_is_enabled(atchan));
  984. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  985. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  986. list_del(&desc->desc_node);
  987. /* free link descriptor */
  988. dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
  989. }
  990. list_splice_init(&atchan->free_list, &list);
  991. atchan->descs_allocated = 0;
  992. atchan->status = 0;
  993. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  994. }
  995. /*-- Module Management -----------------------------------------------*/
  996. /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
  997. static struct at_dma_platform_data at91sam9rl_config = {
  998. .nr_channels = 2,
  999. };
  1000. static struct at_dma_platform_data at91sam9g45_config = {
  1001. .nr_channels = 8,
  1002. };
  1003. #if defined(CONFIG_OF)
  1004. static const struct of_device_id atmel_dma_dt_ids[] = {
  1005. {
  1006. .compatible = "atmel,at91sam9rl-dma",
  1007. .data = &at91sam9rl_config,
  1008. }, {
  1009. .compatible = "atmel,at91sam9g45-dma",
  1010. .data = &at91sam9g45_config,
  1011. }, {
  1012. /* sentinel */
  1013. }
  1014. };
  1015. MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
  1016. #endif
  1017. static const struct platform_device_id atdma_devtypes[] = {
  1018. {
  1019. .name = "at91sam9rl_dma",
  1020. .driver_data = (unsigned long) &at91sam9rl_config,
  1021. }, {
  1022. .name = "at91sam9g45_dma",
  1023. .driver_data = (unsigned long) &at91sam9g45_config,
  1024. }, {
  1025. /* sentinel */
  1026. }
  1027. };
  1028. static inline struct at_dma_platform_data * __init at_dma_get_driver_data(
  1029. struct platform_device *pdev)
  1030. {
  1031. if (pdev->dev.of_node) {
  1032. const struct of_device_id *match;
  1033. match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
  1034. if (match == NULL)
  1035. return NULL;
  1036. return match->data;
  1037. }
  1038. return (struct at_dma_platform_data *)
  1039. platform_get_device_id(pdev)->driver_data;
  1040. }
  1041. /**
  1042. * at_dma_off - disable DMA controller
  1043. * @atdma: the Atmel HDAMC device
  1044. */
  1045. static void at_dma_off(struct at_dma *atdma)
  1046. {
  1047. dma_writel(atdma, EN, 0);
  1048. /* disable all interrupts */
  1049. dma_writel(atdma, EBCIDR, -1L);
  1050. /* confirm that all channels are disabled */
  1051. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  1052. cpu_relax();
  1053. }
  1054. static int __init at_dma_probe(struct platform_device *pdev)
  1055. {
  1056. struct resource *io;
  1057. struct at_dma *atdma;
  1058. size_t size;
  1059. int irq;
  1060. int err;
  1061. int i;
  1062. struct at_dma_platform_data *plat_dat;
  1063. /* setup platform data for each SoC */
  1064. dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
  1065. dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
  1066. dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
  1067. /* get DMA parameters from controller type */
  1068. plat_dat = at_dma_get_driver_data(pdev);
  1069. if (!plat_dat)
  1070. return -ENODEV;
  1071. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1072. if (!io)
  1073. return -EINVAL;
  1074. irq = platform_get_irq(pdev, 0);
  1075. if (irq < 0)
  1076. return irq;
  1077. size = sizeof(struct at_dma);
  1078. size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
  1079. atdma = kzalloc(size, GFP_KERNEL);
  1080. if (!atdma)
  1081. return -ENOMEM;
  1082. /* discover transaction capabilities */
  1083. atdma->dma_common.cap_mask = plat_dat->cap_mask;
  1084. atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
  1085. size = resource_size(io);
  1086. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  1087. err = -EBUSY;
  1088. goto err_kfree;
  1089. }
  1090. atdma->regs = ioremap(io->start, size);
  1091. if (!atdma->regs) {
  1092. err = -ENOMEM;
  1093. goto err_release_r;
  1094. }
  1095. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  1096. if (IS_ERR(atdma->clk)) {
  1097. err = PTR_ERR(atdma->clk);
  1098. goto err_clk;
  1099. }
  1100. clk_enable(atdma->clk);
  1101. /* force dma off, just in case */
  1102. at_dma_off(atdma);
  1103. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  1104. if (err)
  1105. goto err_irq;
  1106. platform_set_drvdata(pdev, atdma);
  1107. /* create a pool of consistent memory blocks for hardware descriptors */
  1108. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  1109. &pdev->dev, sizeof(struct at_desc),
  1110. 4 /* word alignment */, 0);
  1111. if (!atdma->dma_desc_pool) {
  1112. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1113. err = -ENOMEM;
  1114. goto err_pool_create;
  1115. }
  1116. /* clear any pending interrupt */
  1117. while (dma_readl(atdma, EBCISR))
  1118. cpu_relax();
  1119. /* initialize channels related values */
  1120. INIT_LIST_HEAD(&atdma->dma_common.channels);
  1121. for (i = 0; i < plat_dat->nr_channels; i++) {
  1122. struct at_dma_chan *atchan = &atdma->chan[i];
  1123. atchan->chan_common.device = &atdma->dma_common;
  1124. dma_cookie_init(&atchan->chan_common);
  1125. list_add_tail(&atchan->chan_common.device_node,
  1126. &atdma->dma_common.channels);
  1127. atchan->ch_regs = atdma->regs + ch_regs(i);
  1128. spin_lock_init(&atchan->lock);
  1129. atchan->mask = 1 << i;
  1130. INIT_LIST_HEAD(&atchan->active_list);
  1131. INIT_LIST_HEAD(&atchan->queue);
  1132. INIT_LIST_HEAD(&atchan->free_list);
  1133. tasklet_init(&atchan->tasklet, atc_tasklet,
  1134. (unsigned long)atchan);
  1135. atc_enable_chan_irq(atdma, i);
  1136. }
  1137. /* set base routines */
  1138. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  1139. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  1140. atdma->dma_common.device_tx_status = atc_tx_status;
  1141. atdma->dma_common.device_issue_pending = atc_issue_pending;
  1142. atdma->dma_common.dev = &pdev->dev;
  1143. /* set prep routines based on capability */
  1144. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  1145. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  1146. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  1147. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  1148. /* controller can do slave DMA: can trigger cyclic transfers */
  1149. dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
  1150. atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
  1151. atdma->dma_common.device_control = atc_control;
  1152. }
  1153. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1154. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
  1155. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  1156. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  1157. plat_dat->nr_channels);
  1158. dma_async_device_register(&atdma->dma_common);
  1159. return 0;
  1160. err_pool_create:
  1161. platform_set_drvdata(pdev, NULL);
  1162. free_irq(platform_get_irq(pdev, 0), atdma);
  1163. err_irq:
  1164. clk_disable(atdma->clk);
  1165. clk_put(atdma->clk);
  1166. err_clk:
  1167. iounmap(atdma->regs);
  1168. atdma->regs = NULL;
  1169. err_release_r:
  1170. release_mem_region(io->start, size);
  1171. err_kfree:
  1172. kfree(atdma);
  1173. return err;
  1174. }
  1175. static int __exit at_dma_remove(struct platform_device *pdev)
  1176. {
  1177. struct at_dma *atdma = platform_get_drvdata(pdev);
  1178. struct dma_chan *chan, *_chan;
  1179. struct resource *io;
  1180. at_dma_off(atdma);
  1181. dma_async_device_unregister(&atdma->dma_common);
  1182. dma_pool_destroy(atdma->dma_desc_pool);
  1183. platform_set_drvdata(pdev, NULL);
  1184. free_irq(platform_get_irq(pdev, 0), atdma);
  1185. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1186. device_node) {
  1187. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1188. /* Disable interrupts */
  1189. atc_disable_chan_irq(atdma, chan->chan_id);
  1190. tasklet_disable(&atchan->tasklet);
  1191. tasklet_kill(&atchan->tasklet);
  1192. list_del(&chan->device_node);
  1193. }
  1194. clk_disable(atdma->clk);
  1195. clk_put(atdma->clk);
  1196. iounmap(atdma->regs);
  1197. atdma->regs = NULL;
  1198. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1199. release_mem_region(io->start, resource_size(io));
  1200. kfree(atdma);
  1201. return 0;
  1202. }
  1203. static void at_dma_shutdown(struct platform_device *pdev)
  1204. {
  1205. struct at_dma *atdma = platform_get_drvdata(pdev);
  1206. at_dma_off(platform_get_drvdata(pdev));
  1207. clk_disable(atdma->clk);
  1208. }
  1209. static int at_dma_prepare(struct device *dev)
  1210. {
  1211. struct platform_device *pdev = to_platform_device(dev);
  1212. struct at_dma *atdma = platform_get_drvdata(pdev);
  1213. struct dma_chan *chan, *_chan;
  1214. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1215. device_node) {
  1216. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1217. /* wait for transaction completion (except in cyclic case) */
  1218. if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
  1219. return -EAGAIN;
  1220. }
  1221. return 0;
  1222. }
  1223. static void atc_suspend_cyclic(struct at_dma_chan *atchan)
  1224. {
  1225. struct dma_chan *chan = &atchan->chan_common;
  1226. /* Channel should be paused by user
  1227. * do it anyway even if it is not done already */
  1228. if (!atc_chan_is_paused(atchan)) {
  1229. dev_warn(chan2dev(chan),
  1230. "cyclic channel not paused, should be done by channel user\n");
  1231. atc_control(chan, DMA_PAUSE, 0);
  1232. }
  1233. /* now preserve additional data for cyclic operations */
  1234. /* next descriptor address in the cyclic list */
  1235. atchan->save_dscr = channel_readl(atchan, DSCR);
  1236. vdbg_dump_regs(atchan);
  1237. }
  1238. static int at_dma_suspend_noirq(struct device *dev)
  1239. {
  1240. struct platform_device *pdev = to_platform_device(dev);
  1241. struct at_dma *atdma = platform_get_drvdata(pdev);
  1242. struct dma_chan *chan, *_chan;
  1243. /* preserve data */
  1244. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1245. device_node) {
  1246. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1247. if (atc_chan_is_cyclic(atchan))
  1248. atc_suspend_cyclic(atchan);
  1249. atchan->save_cfg = channel_readl(atchan, CFG);
  1250. }
  1251. atdma->save_imr = dma_readl(atdma, EBCIMR);
  1252. /* disable DMA controller */
  1253. at_dma_off(atdma);
  1254. clk_disable(atdma->clk);
  1255. return 0;
  1256. }
  1257. static void atc_resume_cyclic(struct at_dma_chan *atchan)
  1258. {
  1259. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  1260. /* restore channel status for cyclic descriptors list:
  1261. * next descriptor in the cyclic list at the time of suspend */
  1262. channel_writel(atchan, SADDR, 0);
  1263. channel_writel(atchan, DADDR, 0);
  1264. channel_writel(atchan, CTRLA, 0);
  1265. channel_writel(atchan, CTRLB, 0);
  1266. channel_writel(atchan, DSCR, atchan->save_dscr);
  1267. dma_writel(atdma, CHER, atchan->mask);
  1268. /* channel pause status should be removed by channel user
  1269. * We cannot take the initiative to do it here */
  1270. vdbg_dump_regs(atchan);
  1271. }
  1272. static int at_dma_resume_noirq(struct device *dev)
  1273. {
  1274. struct platform_device *pdev = to_platform_device(dev);
  1275. struct at_dma *atdma = platform_get_drvdata(pdev);
  1276. struct dma_chan *chan, *_chan;
  1277. /* bring back DMA controller */
  1278. clk_enable(atdma->clk);
  1279. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1280. /* clear any pending interrupt */
  1281. while (dma_readl(atdma, EBCISR))
  1282. cpu_relax();
  1283. /* restore saved data */
  1284. dma_writel(atdma, EBCIER, atdma->save_imr);
  1285. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1286. device_node) {
  1287. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1288. channel_writel(atchan, CFG, atchan->save_cfg);
  1289. if (atc_chan_is_cyclic(atchan))
  1290. atc_resume_cyclic(atchan);
  1291. }
  1292. return 0;
  1293. }
  1294. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  1295. .prepare = at_dma_prepare,
  1296. .suspend_noirq = at_dma_suspend_noirq,
  1297. .resume_noirq = at_dma_resume_noirq,
  1298. };
  1299. static struct platform_driver at_dma_driver = {
  1300. .remove = __exit_p(at_dma_remove),
  1301. .shutdown = at_dma_shutdown,
  1302. .id_table = atdma_devtypes,
  1303. .driver = {
  1304. .name = "at_hdmac",
  1305. .pm = &at_dma_dev_pm_ops,
  1306. .of_match_table = of_match_ptr(atmel_dma_dt_ids),
  1307. },
  1308. };
  1309. static int __init at_dma_init(void)
  1310. {
  1311. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1312. }
  1313. subsys_initcall(at_dma_init);
  1314. static void __exit at_dma_exit(void)
  1315. {
  1316. platform_driver_unregister(&at_dma_driver);
  1317. }
  1318. module_exit(at_dma_exit);
  1319. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1320. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1321. MODULE_LICENSE("GPL");
  1322. MODULE_ALIAS("platform:at_hdmac");