at_hdmac.c 32 KB

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