at_hdmac.c 40 KB

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