at_hdmac.c 42 KB

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