at_hdmac.c 37 KB

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