at_hdmac.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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 int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd)
  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. /* Only supports DMA_TERMINATE_ALL */
  655. if (cmd != DMA_TERMINATE_ALL)
  656. return -ENXIO;
  657. /*
  658. * This is only called when something went wrong elsewhere, so
  659. * we don't really care about the data. Just disable the
  660. * channel. We still have to poll the channel enable bit due
  661. * to AHB/HSB limitations.
  662. */
  663. spin_lock_bh(&atchan->lock);
  664. dma_writel(atdma, CHDR, atchan->mask);
  665. /* confirm that this channel is disabled */
  666. while (dma_readl(atdma, CHSR) & atchan->mask)
  667. cpu_relax();
  668. /* active_list entries will end up before queued entries */
  669. list_splice_init(&atchan->queue, &list);
  670. list_splice_init(&atchan->active_list, &list);
  671. spin_unlock_bh(&atchan->lock);
  672. /* Flush all pending and queued descriptors */
  673. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  674. atc_chain_complete(atchan, desc);
  675. return 0;
  676. }
  677. /**
  678. * atc_tx_status - poll for transaction completion
  679. * @chan: DMA channel
  680. * @cookie: transaction identifier to check status of
  681. * @txstate: if not %NULL updated with transaction state
  682. *
  683. * If @txstate is passed in, upon return it reflect the driver
  684. * internal state and can be used with dma_async_is_complete() to check
  685. * the status of multiple cookies without re-checking hardware state.
  686. */
  687. static enum dma_status
  688. atc_tx_status(struct dma_chan *chan,
  689. dma_cookie_t cookie,
  690. struct dma_tx_state *txstate)
  691. {
  692. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  693. dma_cookie_t last_used;
  694. dma_cookie_t last_complete;
  695. enum dma_status ret;
  696. spin_lock_bh(&atchan->lock);
  697. last_complete = atchan->completed_cookie;
  698. last_used = chan->cookie;
  699. ret = dma_async_is_complete(cookie, last_complete, last_used);
  700. if (ret != DMA_SUCCESS) {
  701. atc_cleanup_descriptors(atchan);
  702. last_complete = atchan->completed_cookie;
  703. last_used = chan->cookie;
  704. ret = dma_async_is_complete(cookie, last_complete, last_used);
  705. }
  706. spin_unlock_bh(&atchan->lock);
  707. dma_set_tx_state(txstate, last_complete, last_used, 0);
  708. dev_vdbg(chan2dev(chan), "tx_status: %d (d%d, u%d)\n",
  709. cookie, last_complete ? last_complete : 0,
  710. last_used ? last_used : 0);
  711. return ret;
  712. }
  713. /**
  714. * atc_issue_pending - try to finish work
  715. * @chan: target DMA channel
  716. */
  717. static void atc_issue_pending(struct dma_chan *chan)
  718. {
  719. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  720. dev_vdbg(chan2dev(chan), "issue_pending\n");
  721. if (!atc_chan_is_enabled(atchan)) {
  722. spin_lock_bh(&atchan->lock);
  723. atc_advance_work(atchan);
  724. spin_unlock_bh(&atchan->lock);
  725. }
  726. }
  727. /**
  728. * atc_alloc_chan_resources - allocate resources for DMA channel
  729. * @chan: allocate descriptor resources for this channel
  730. * @client: current client requesting the channel be ready for requests
  731. *
  732. * return - the number of allocated descriptors
  733. */
  734. static int atc_alloc_chan_resources(struct dma_chan *chan)
  735. {
  736. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  737. struct at_dma *atdma = to_at_dma(chan->device);
  738. struct at_desc *desc;
  739. struct at_dma_slave *atslave;
  740. int i;
  741. u32 cfg;
  742. LIST_HEAD(tmp_list);
  743. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  744. /* ASSERT: channel is idle */
  745. if (atc_chan_is_enabled(atchan)) {
  746. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  747. return -EIO;
  748. }
  749. cfg = ATC_DEFAULT_CFG;
  750. atslave = chan->private;
  751. if (atslave) {
  752. /*
  753. * We need controller-specific data to set up slave
  754. * transfers.
  755. */
  756. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
  757. /* if cfg configuration specified take it instad of default */
  758. if (atslave->cfg)
  759. cfg = atslave->cfg;
  760. }
  761. /* have we already been set up?
  762. * reconfigure channel but no need to reallocate descriptors */
  763. if (!list_empty(&atchan->free_list))
  764. return atchan->descs_allocated;
  765. /* Allocate initial pool of descriptors */
  766. for (i = 0; i < init_nr_desc_per_channel; i++) {
  767. desc = atc_alloc_descriptor(chan, GFP_KERNEL);
  768. if (!desc) {
  769. dev_err(atdma->dma_common.dev,
  770. "Only %d initial descriptors\n", i);
  771. break;
  772. }
  773. list_add_tail(&desc->desc_node, &tmp_list);
  774. }
  775. spin_lock_bh(&atchan->lock);
  776. atchan->descs_allocated = i;
  777. list_splice(&tmp_list, &atchan->free_list);
  778. atchan->completed_cookie = chan->cookie = 1;
  779. spin_unlock_bh(&atchan->lock);
  780. /* channel parameters */
  781. channel_writel(atchan, CFG, cfg);
  782. dev_dbg(chan2dev(chan),
  783. "alloc_chan_resources: allocated %d descriptors\n",
  784. atchan->descs_allocated);
  785. return atchan->descs_allocated;
  786. }
  787. /**
  788. * atc_free_chan_resources - free all channel resources
  789. * @chan: DMA channel
  790. */
  791. static void atc_free_chan_resources(struct dma_chan *chan)
  792. {
  793. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  794. struct at_dma *atdma = to_at_dma(chan->device);
  795. struct at_desc *desc, *_desc;
  796. LIST_HEAD(list);
  797. dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
  798. atchan->descs_allocated);
  799. /* ASSERT: channel is idle */
  800. BUG_ON(!list_empty(&atchan->active_list));
  801. BUG_ON(!list_empty(&atchan->queue));
  802. BUG_ON(atc_chan_is_enabled(atchan));
  803. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  804. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  805. list_del(&desc->desc_node);
  806. /* free link descriptor */
  807. dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
  808. }
  809. list_splice_init(&atchan->free_list, &list);
  810. atchan->descs_allocated = 0;
  811. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  812. }
  813. /*-- Module Management -----------------------------------------------*/
  814. /**
  815. * at_dma_off - disable DMA controller
  816. * @atdma: the Atmel HDAMC device
  817. */
  818. static void at_dma_off(struct at_dma *atdma)
  819. {
  820. dma_writel(atdma, EN, 0);
  821. /* disable all interrupts */
  822. dma_writel(atdma, EBCIDR, -1L);
  823. /* confirm that all channels are disabled */
  824. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  825. cpu_relax();
  826. }
  827. static int __init at_dma_probe(struct platform_device *pdev)
  828. {
  829. struct at_dma_platform_data *pdata;
  830. struct resource *io;
  831. struct at_dma *atdma;
  832. size_t size;
  833. int irq;
  834. int err;
  835. int i;
  836. /* get DMA Controller parameters from platform */
  837. pdata = pdev->dev.platform_data;
  838. if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
  839. return -EINVAL;
  840. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  841. if (!io)
  842. return -EINVAL;
  843. irq = platform_get_irq(pdev, 0);
  844. if (irq < 0)
  845. return irq;
  846. size = sizeof(struct at_dma);
  847. size += pdata->nr_channels * sizeof(struct at_dma_chan);
  848. atdma = kzalloc(size, GFP_KERNEL);
  849. if (!atdma)
  850. return -ENOMEM;
  851. /* discover transaction capabilites from the platform data */
  852. atdma->dma_common.cap_mask = pdata->cap_mask;
  853. atdma->all_chan_mask = (1 << pdata->nr_channels) - 1;
  854. size = io->end - io->start + 1;
  855. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  856. err = -EBUSY;
  857. goto err_kfree;
  858. }
  859. atdma->regs = ioremap(io->start, size);
  860. if (!atdma->regs) {
  861. err = -ENOMEM;
  862. goto err_release_r;
  863. }
  864. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  865. if (IS_ERR(atdma->clk)) {
  866. err = PTR_ERR(atdma->clk);
  867. goto err_clk;
  868. }
  869. clk_enable(atdma->clk);
  870. /* force dma off, just in case */
  871. at_dma_off(atdma);
  872. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  873. if (err)
  874. goto err_irq;
  875. platform_set_drvdata(pdev, atdma);
  876. /* create a pool of consistent memory blocks for hardware descriptors */
  877. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  878. &pdev->dev, sizeof(struct at_desc),
  879. 4 /* word alignment */, 0);
  880. if (!atdma->dma_desc_pool) {
  881. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  882. err = -ENOMEM;
  883. goto err_pool_create;
  884. }
  885. /* clear any pending interrupt */
  886. while (dma_readl(atdma, EBCISR))
  887. cpu_relax();
  888. /* initialize channels related values */
  889. INIT_LIST_HEAD(&atdma->dma_common.channels);
  890. for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) {
  891. struct at_dma_chan *atchan = &atdma->chan[i];
  892. atchan->chan_common.device = &atdma->dma_common;
  893. atchan->chan_common.cookie = atchan->completed_cookie = 1;
  894. atchan->chan_common.chan_id = i;
  895. list_add_tail(&atchan->chan_common.device_node,
  896. &atdma->dma_common.channels);
  897. atchan->ch_regs = atdma->regs + ch_regs(i);
  898. spin_lock_init(&atchan->lock);
  899. atchan->mask = 1 << i;
  900. INIT_LIST_HEAD(&atchan->active_list);
  901. INIT_LIST_HEAD(&atchan->queue);
  902. INIT_LIST_HEAD(&atchan->free_list);
  903. tasklet_init(&atchan->tasklet, atc_tasklet,
  904. (unsigned long)atchan);
  905. atc_enable_irq(atchan);
  906. }
  907. /* set base routines */
  908. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  909. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  910. atdma->dma_common.device_tx_status = atc_tx_status;
  911. atdma->dma_common.device_issue_pending = atc_issue_pending;
  912. atdma->dma_common.dev = &pdev->dev;
  913. /* set prep routines based on capability */
  914. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  915. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  916. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  917. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  918. atdma->dma_common.device_control = atc_control;
  919. }
  920. dma_writel(atdma, EN, AT_DMA_ENABLE);
  921. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
  922. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  923. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  924. atdma->dma_common.chancnt);
  925. dma_async_device_register(&atdma->dma_common);
  926. return 0;
  927. err_pool_create:
  928. platform_set_drvdata(pdev, NULL);
  929. free_irq(platform_get_irq(pdev, 0), atdma);
  930. err_irq:
  931. clk_disable(atdma->clk);
  932. clk_put(atdma->clk);
  933. err_clk:
  934. iounmap(atdma->regs);
  935. atdma->regs = NULL;
  936. err_release_r:
  937. release_mem_region(io->start, size);
  938. err_kfree:
  939. kfree(atdma);
  940. return err;
  941. }
  942. static int __exit at_dma_remove(struct platform_device *pdev)
  943. {
  944. struct at_dma *atdma = platform_get_drvdata(pdev);
  945. struct dma_chan *chan, *_chan;
  946. struct resource *io;
  947. at_dma_off(atdma);
  948. dma_async_device_unregister(&atdma->dma_common);
  949. dma_pool_destroy(atdma->dma_desc_pool);
  950. platform_set_drvdata(pdev, NULL);
  951. free_irq(platform_get_irq(pdev, 0), atdma);
  952. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  953. device_node) {
  954. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  955. /* Disable interrupts */
  956. atc_disable_irq(atchan);
  957. tasklet_disable(&atchan->tasklet);
  958. tasklet_kill(&atchan->tasklet);
  959. list_del(&chan->device_node);
  960. }
  961. clk_disable(atdma->clk);
  962. clk_put(atdma->clk);
  963. iounmap(atdma->regs);
  964. atdma->regs = NULL;
  965. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  966. release_mem_region(io->start, io->end - io->start + 1);
  967. kfree(atdma);
  968. return 0;
  969. }
  970. static void at_dma_shutdown(struct platform_device *pdev)
  971. {
  972. struct at_dma *atdma = platform_get_drvdata(pdev);
  973. at_dma_off(platform_get_drvdata(pdev));
  974. clk_disable(atdma->clk);
  975. }
  976. static int at_dma_suspend_noirq(struct device *dev)
  977. {
  978. struct platform_device *pdev = to_platform_device(dev);
  979. struct at_dma *atdma = platform_get_drvdata(pdev);
  980. at_dma_off(platform_get_drvdata(pdev));
  981. clk_disable(atdma->clk);
  982. return 0;
  983. }
  984. static int at_dma_resume_noirq(struct device *dev)
  985. {
  986. struct platform_device *pdev = to_platform_device(dev);
  987. struct at_dma *atdma = platform_get_drvdata(pdev);
  988. clk_enable(atdma->clk);
  989. dma_writel(atdma, EN, AT_DMA_ENABLE);
  990. return 0;
  991. }
  992. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  993. .suspend_noirq = at_dma_suspend_noirq,
  994. .resume_noirq = at_dma_resume_noirq,
  995. };
  996. static struct platform_driver at_dma_driver = {
  997. .remove = __exit_p(at_dma_remove),
  998. .shutdown = at_dma_shutdown,
  999. .driver = {
  1000. .name = "at_hdmac",
  1001. .pm = &at_dma_dev_pm_ops,
  1002. },
  1003. };
  1004. static int __init at_dma_init(void)
  1005. {
  1006. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1007. }
  1008. module_init(at_dma_init);
  1009. static void __exit at_dma_exit(void)
  1010. {
  1011. platform_driver_unregister(&at_dma_driver);
  1012. }
  1013. module_exit(at_dma_exit);
  1014. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1015. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1016. MODULE_LICENSE("GPL");
  1017. MODULE_ALIAS("platform:at_hdmac");