at_hdmac.c 42 KB

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