at_hdmac.c 42 KB

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