at_hdmac.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  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 found in several Atmel SoCs.
  13. * The only Atmel DMA Controller that is not covered by this driver is the one
  14. * found on AT91SAM9263.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/dmapool.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/of_dma.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. static void atc_issue_pending(struct dma_chan *chan);
  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 descriptor
  153. * @first: address of first descriptor of the chain
  154. * @prev: address of previous descriptor 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. channel_writel(atchan, SADDR, 0);
  199. channel_writel(atchan, DADDR, 0);
  200. channel_writel(atchan, CTRLA, 0);
  201. channel_writel(atchan, CTRLB, 0);
  202. channel_writel(atchan, DSCR, first->txd.phys);
  203. dma_writel(atdma, CHER, atchan->mask);
  204. vdbg_dump_regs(atchan);
  205. }
  206. /*
  207. * atc_get_current_descriptors -
  208. * locate the descriptor which equal to physical address in DSCR
  209. * @atchan: the channel we want to start
  210. * @dscr_addr: physical descriptor address in DSCR
  211. */
  212. static struct at_desc *atc_get_current_descriptors(struct at_dma_chan *atchan,
  213. u32 dscr_addr)
  214. {
  215. struct at_desc *desc, *_desc, *child, *desc_cur = NULL;
  216. list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
  217. if (desc->lli.dscr == dscr_addr) {
  218. desc_cur = desc;
  219. break;
  220. }
  221. list_for_each_entry(child, &desc->tx_list, desc_node) {
  222. if (child->lli.dscr == dscr_addr) {
  223. desc_cur = child;
  224. break;
  225. }
  226. }
  227. }
  228. return desc_cur;
  229. }
  230. /*
  231. * atc_get_bytes_left -
  232. * Get the number of bytes residue in dma buffer,
  233. * @chan: the channel we want to start
  234. */
  235. static int atc_get_bytes_left(struct dma_chan *chan)
  236. {
  237. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  238. struct at_dma *atdma = to_at_dma(chan->device);
  239. int chan_id = atchan->chan_common.chan_id;
  240. struct at_desc *desc_first = atc_first_active(atchan);
  241. struct at_desc *desc_cur;
  242. int ret = 0, count = 0;
  243. /*
  244. * Initialize necessary values in the first time.
  245. * remain_desc record remain desc length.
  246. */
  247. if (atchan->remain_desc == 0)
  248. /* First descriptor embedds the transaction length */
  249. atchan->remain_desc = desc_first->len;
  250. /*
  251. * This happens when current descriptor transfer complete.
  252. * The residual buffer size should reduce current descriptor length.
  253. */
  254. if (unlikely(test_bit(ATC_IS_BTC, &atchan->status))) {
  255. clear_bit(ATC_IS_BTC, &atchan->status);
  256. desc_cur = atc_get_current_descriptors(atchan,
  257. channel_readl(atchan, DSCR));
  258. if (!desc_cur) {
  259. ret = -EINVAL;
  260. goto out;
  261. }
  262. atchan->remain_desc -= (desc_cur->lli.ctrla & ATC_BTSIZE_MAX)
  263. << (desc_first->tx_width);
  264. if (atchan->remain_desc < 0) {
  265. ret = -EINVAL;
  266. goto out;
  267. } else
  268. ret = atchan->remain_desc;
  269. } else {
  270. /*
  271. * Get residual bytes when current
  272. * descriptor transfer in progress.
  273. */
  274. count = (channel_readl(atchan, CTRLA) & ATC_BTSIZE_MAX)
  275. << (desc_first->tx_width);
  276. ret = atchan->remain_desc - count;
  277. }
  278. /*
  279. * Check fifo empty.
  280. */
  281. if (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id)))
  282. atc_issue_pending(chan);
  283. out:
  284. return ret;
  285. }
  286. /**
  287. * atc_chain_complete - finish work for one transaction chain
  288. * @atchan: channel we work on
  289. * @desc: descriptor at the head of the chain we want do complete
  290. *
  291. * Called with atchan->lock held and bh disabled */
  292. static void
  293. atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
  294. {
  295. struct dma_async_tx_descriptor *txd = &desc->txd;
  296. dev_vdbg(chan2dev(&atchan->chan_common),
  297. "descriptor %u complete\n", txd->cookie);
  298. /* mark the descriptor as complete for non cyclic cases only */
  299. if (!atc_chan_is_cyclic(atchan))
  300. dma_cookie_complete(txd);
  301. /* move children to free_list */
  302. list_splice_init(&desc->tx_list, &atchan->free_list);
  303. /* move myself to free_list */
  304. list_move(&desc->desc_node, &atchan->free_list);
  305. /* unmap dma addresses (not on slave channels) */
  306. if (!atchan->chan_common.private) {
  307. struct device *parent = chan2parent(&atchan->chan_common);
  308. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  309. if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  310. dma_unmap_single(parent,
  311. desc->lli.daddr,
  312. desc->len, DMA_FROM_DEVICE);
  313. else
  314. dma_unmap_page(parent,
  315. desc->lli.daddr,
  316. desc->len, DMA_FROM_DEVICE);
  317. }
  318. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  319. if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  320. dma_unmap_single(parent,
  321. desc->lli.saddr,
  322. desc->len, DMA_TO_DEVICE);
  323. else
  324. dma_unmap_page(parent,
  325. desc->lli.saddr,
  326. desc->len, DMA_TO_DEVICE);
  327. }
  328. }
  329. /* for cyclic transfers,
  330. * no need to replay callback function while stopping */
  331. if (!atc_chan_is_cyclic(atchan)) {
  332. dma_async_tx_callback callback = txd->callback;
  333. void *param = txd->callback_param;
  334. /*
  335. * The API requires that no submissions are done from a
  336. * callback, so we don't need to drop the lock here
  337. */
  338. if (callback)
  339. callback(param);
  340. }
  341. dma_run_dependencies(txd);
  342. }
  343. /**
  344. * atc_complete_all - finish work for all transactions
  345. * @atchan: channel to complete transactions for
  346. *
  347. * Eventually submit queued descriptors if any
  348. *
  349. * Assume channel is idle while calling this function
  350. * Called with atchan->lock held and bh disabled
  351. */
  352. static void atc_complete_all(struct at_dma_chan *atchan)
  353. {
  354. struct at_desc *desc, *_desc;
  355. LIST_HEAD(list);
  356. dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
  357. /*
  358. * Submit queued descriptors ASAP, i.e. before we go through
  359. * the completed ones.
  360. */
  361. if (!list_empty(&atchan->queue))
  362. atc_dostart(atchan, atc_first_queued(atchan));
  363. /* empty active_list now it is completed */
  364. list_splice_init(&atchan->active_list, &list);
  365. /* empty queue list by moving descriptors (if any) to active_list */
  366. list_splice_init(&atchan->queue, &atchan->active_list);
  367. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  368. atc_chain_complete(atchan, desc);
  369. }
  370. /**
  371. * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
  372. * @atchan: channel to be cleaned up
  373. *
  374. * Called with atchan->lock held and bh disabled
  375. */
  376. static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
  377. {
  378. struct at_desc *desc, *_desc;
  379. struct at_desc *child;
  380. dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
  381. list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
  382. if (!(desc->lli.ctrla & ATC_DONE))
  383. /* This one is currently in progress */
  384. return;
  385. list_for_each_entry(child, &desc->tx_list, desc_node)
  386. if (!(child->lli.ctrla & ATC_DONE))
  387. /* Currently in progress */
  388. return;
  389. /*
  390. * No descriptors so far seem to be in progress, i.e.
  391. * this chain must be done.
  392. */
  393. atc_chain_complete(atchan, desc);
  394. }
  395. }
  396. /**
  397. * atc_advance_work - at the end of a transaction, move forward
  398. * @atchan: channel where the transaction ended
  399. *
  400. * Called with atchan->lock held and bh disabled
  401. */
  402. static void atc_advance_work(struct at_dma_chan *atchan)
  403. {
  404. dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
  405. if (atc_chan_is_enabled(atchan))
  406. return;
  407. if (list_empty(&atchan->active_list) ||
  408. list_is_singular(&atchan->active_list)) {
  409. atc_complete_all(atchan);
  410. } else {
  411. atc_chain_complete(atchan, atc_first_active(atchan));
  412. /* advance work */
  413. atc_dostart(atchan, atc_first_active(atchan));
  414. }
  415. }
  416. /**
  417. * atc_handle_error - handle errors reported by DMA controller
  418. * @atchan: channel where error occurs
  419. *
  420. * Called with atchan->lock held and bh disabled
  421. */
  422. static void atc_handle_error(struct at_dma_chan *atchan)
  423. {
  424. struct at_desc *bad_desc;
  425. struct at_desc *child;
  426. /*
  427. * The descriptor currently at the head of the active list is
  428. * broked. Since we don't have any way to report errors, we'll
  429. * just have to scream loudly and try to carry on.
  430. */
  431. bad_desc = atc_first_active(atchan);
  432. list_del_init(&bad_desc->desc_node);
  433. /* As we are stopped, take advantage to push queued descriptors
  434. * in active_list */
  435. list_splice_init(&atchan->queue, atchan->active_list.prev);
  436. /* Try to restart the controller */
  437. if (!list_empty(&atchan->active_list))
  438. atc_dostart(atchan, atc_first_active(atchan));
  439. /*
  440. * KERN_CRITICAL may seem harsh, but since this only happens
  441. * when someone submits a bad physical address in a
  442. * descriptor, we should consider ourselves lucky that the
  443. * controller flagged an error instead of scribbling over
  444. * random memory locations.
  445. */
  446. dev_crit(chan2dev(&atchan->chan_common),
  447. "Bad descriptor submitted for DMA!\n");
  448. dev_crit(chan2dev(&atchan->chan_common),
  449. " cookie: %d\n", bad_desc->txd.cookie);
  450. atc_dump_lli(atchan, &bad_desc->lli);
  451. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  452. atc_dump_lli(atchan, &child->lli);
  453. /* Pretend the descriptor completed successfully */
  454. atc_chain_complete(atchan, bad_desc);
  455. }
  456. /**
  457. * atc_handle_cyclic - at the end of a period, run callback function
  458. * @atchan: channel used for cyclic operations
  459. *
  460. * Called with atchan->lock held and bh disabled
  461. */
  462. static void atc_handle_cyclic(struct at_dma_chan *atchan)
  463. {
  464. struct at_desc *first = atc_first_active(atchan);
  465. struct dma_async_tx_descriptor *txd = &first->txd;
  466. dma_async_tx_callback callback = txd->callback;
  467. void *param = txd->callback_param;
  468. dev_vdbg(chan2dev(&atchan->chan_common),
  469. "new cyclic period llp 0x%08x\n",
  470. channel_readl(atchan, DSCR));
  471. if (callback)
  472. callback(param);
  473. }
  474. /*-- IRQ & Tasklet ---------------------------------------------------*/
  475. static void atc_tasklet(unsigned long data)
  476. {
  477. struct at_dma_chan *atchan = (struct at_dma_chan *)data;
  478. unsigned long flags;
  479. spin_lock_irqsave(&atchan->lock, flags);
  480. if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status))
  481. atc_handle_error(atchan);
  482. else if (atc_chan_is_cyclic(atchan))
  483. atc_handle_cyclic(atchan);
  484. else
  485. atc_advance_work(atchan);
  486. spin_unlock_irqrestore(&atchan->lock, flags);
  487. }
  488. static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
  489. {
  490. struct at_dma *atdma = (struct at_dma *)dev_id;
  491. struct at_dma_chan *atchan;
  492. int i;
  493. u32 status, pending, imr;
  494. int ret = IRQ_NONE;
  495. do {
  496. imr = dma_readl(atdma, EBCIMR);
  497. status = dma_readl(atdma, EBCISR);
  498. pending = status & imr;
  499. if (!pending)
  500. break;
  501. dev_vdbg(atdma->dma_common.dev,
  502. "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
  503. status, imr, pending);
  504. for (i = 0; i < atdma->dma_common.chancnt; i++) {
  505. atchan = &atdma->chan[i];
  506. if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) {
  507. if (pending & AT_DMA_ERR(i)) {
  508. /* Disable channel on AHB error */
  509. dma_writel(atdma, CHDR,
  510. AT_DMA_RES(i) | atchan->mask);
  511. /* Give information to tasklet */
  512. set_bit(ATC_IS_ERROR, &atchan->status);
  513. }
  514. if (pending & AT_DMA_BTC(i))
  515. set_bit(ATC_IS_BTC, &atchan->status);
  516. tasklet_schedule(&atchan->tasklet);
  517. ret = IRQ_HANDLED;
  518. }
  519. }
  520. } while (pending);
  521. return ret;
  522. }
  523. /*-- DMA Engine API --------------------------------------------------*/
  524. /**
  525. * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
  526. * @desc: descriptor at the head of the transaction chain
  527. *
  528. * Queue chain if DMA engine is working already
  529. *
  530. * Cookie increment and adding to active_list or queue must be atomic
  531. */
  532. static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
  533. {
  534. struct at_desc *desc = txd_to_at_desc(tx);
  535. struct at_dma_chan *atchan = to_at_dma_chan(tx->chan);
  536. dma_cookie_t cookie;
  537. unsigned long flags;
  538. spin_lock_irqsave(&atchan->lock, flags);
  539. cookie = dma_cookie_assign(tx);
  540. if (list_empty(&atchan->active_list)) {
  541. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  542. desc->txd.cookie);
  543. atc_dostart(atchan, desc);
  544. list_add_tail(&desc->desc_node, &atchan->active_list);
  545. } else {
  546. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  547. desc->txd.cookie);
  548. list_add_tail(&desc->desc_node, &atchan->queue);
  549. }
  550. spin_unlock_irqrestore(&atchan->lock, flags);
  551. return cookie;
  552. }
  553. /**
  554. * atc_prep_dma_memcpy - prepare a memcpy operation
  555. * @chan: the channel to prepare operation on
  556. * @dest: operation virtual destination address
  557. * @src: operation virtual source address
  558. * @len: operation length
  559. * @flags: tx descriptor status flags
  560. */
  561. static struct dma_async_tx_descriptor *
  562. atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  563. size_t len, unsigned long flags)
  564. {
  565. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  566. struct at_desc *desc = NULL;
  567. struct at_desc *first = NULL;
  568. struct at_desc *prev = NULL;
  569. size_t xfer_count;
  570. size_t offset;
  571. unsigned int src_width;
  572. unsigned int dst_width;
  573. u32 ctrla;
  574. u32 ctrlb;
  575. dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
  576. dest, src, len, flags);
  577. if (unlikely(!len)) {
  578. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  579. return NULL;
  580. }
  581. ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN
  582. | ATC_SRC_ADDR_MODE_INCR
  583. | ATC_DST_ADDR_MODE_INCR
  584. | ATC_FC_MEM2MEM;
  585. /*
  586. * We can be a lot more clever here, but this should take care
  587. * of the most common optimization.
  588. */
  589. if (!((src | dest | len) & 3)) {
  590. ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
  591. src_width = dst_width = 2;
  592. } else if (!((src | dest | len) & 1)) {
  593. ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
  594. src_width = dst_width = 1;
  595. } else {
  596. ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
  597. src_width = dst_width = 0;
  598. }
  599. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  600. xfer_count = min_t(size_t, (len - offset) >> src_width,
  601. ATC_BTSIZE_MAX);
  602. desc = atc_desc_get(atchan);
  603. if (!desc)
  604. goto err_desc_get;
  605. desc->lli.saddr = src + offset;
  606. desc->lli.daddr = dest + offset;
  607. desc->lli.ctrla = ctrla | xfer_count;
  608. desc->lli.ctrlb = ctrlb;
  609. desc->txd.cookie = 0;
  610. atc_desc_chain(&first, &prev, desc);
  611. }
  612. /* First descriptor of the chain embedds additional information */
  613. first->txd.cookie = -EBUSY;
  614. first->len = len;
  615. first->tx_width = src_width;
  616. /* set end-of-link to the last link descriptor of list*/
  617. set_desc_eol(desc);
  618. first->txd.flags = flags; /* client is in control of this ack */
  619. return &first->txd;
  620. err_desc_get:
  621. atc_desc_put(atchan, first);
  622. return NULL;
  623. }
  624. /**
  625. * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
  626. * @chan: DMA channel
  627. * @sgl: scatterlist to transfer to/from
  628. * @sg_len: number of entries in @scatterlist
  629. * @direction: DMA direction
  630. * @flags: tx descriptor status flags
  631. * @context: transaction context (ignored)
  632. */
  633. static struct dma_async_tx_descriptor *
  634. atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  635. unsigned int sg_len, enum dma_transfer_direction direction,
  636. unsigned long flags, void *context)
  637. {
  638. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  639. struct at_dma_slave *atslave = chan->private;
  640. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  641. struct at_desc *first = NULL;
  642. struct at_desc *prev = NULL;
  643. u32 ctrla;
  644. u32 ctrlb;
  645. dma_addr_t reg;
  646. unsigned int reg_width;
  647. unsigned int mem_width;
  648. unsigned int i;
  649. struct scatterlist *sg;
  650. size_t total_len = 0;
  651. dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n",
  652. sg_len,
  653. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  654. flags);
  655. if (unlikely(!atslave || !sg_len)) {
  656. dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n");
  657. return NULL;
  658. }
  659. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  660. | ATC_DCSIZE(sconfig->dst_maxburst);
  661. ctrlb = ATC_IEN;
  662. switch (direction) {
  663. case DMA_MEM_TO_DEV:
  664. reg_width = convert_buswidth(sconfig->dst_addr_width);
  665. ctrla |= ATC_DST_WIDTH(reg_width);
  666. ctrlb |= ATC_DST_ADDR_MODE_FIXED
  667. | ATC_SRC_ADDR_MODE_INCR
  668. | ATC_FC_MEM2PER
  669. | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if);
  670. reg = sconfig->dst_addr;
  671. for_each_sg(sgl, sg, sg_len, i) {
  672. struct at_desc *desc;
  673. u32 len;
  674. u32 mem;
  675. desc = atc_desc_get(atchan);
  676. if (!desc)
  677. goto err_desc_get;
  678. mem = sg_dma_address(sg);
  679. len = sg_dma_len(sg);
  680. if (unlikely(!len)) {
  681. dev_dbg(chan2dev(chan),
  682. "prep_slave_sg: sg(%d) data length is zero\n", i);
  683. goto err;
  684. }
  685. mem_width = 2;
  686. if (unlikely(mem & 3 || len & 3))
  687. mem_width = 0;
  688. desc->lli.saddr = mem;
  689. desc->lli.daddr = reg;
  690. desc->lli.ctrla = ctrla
  691. | ATC_SRC_WIDTH(mem_width)
  692. | len >> mem_width;
  693. desc->lli.ctrlb = ctrlb;
  694. atc_desc_chain(&first, &prev, desc);
  695. total_len += len;
  696. }
  697. break;
  698. case DMA_DEV_TO_MEM:
  699. reg_width = convert_buswidth(sconfig->src_addr_width);
  700. ctrla |= ATC_SRC_WIDTH(reg_width);
  701. ctrlb |= ATC_DST_ADDR_MODE_INCR
  702. | ATC_SRC_ADDR_MODE_FIXED
  703. | ATC_FC_PER2MEM
  704. | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if);
  705. reg = sconfig->src_addr;
  706. for_each_sg(sgl, sg, sg_len, i) {
  707. struct at_desc *desc;
  708. u32 len;
  709. u32 mem;
  710. desc = atc_desc_get(atchan);
  711. if (!desc)
  712. goto err_desc_get;
  713. mem = sg_dma_address(sg);
  714. len = sg_dma_len(sg);
  715. if (unlikely(!len)) {
  716. dev_dbg(chan2dev(chan),
  717. "prep_slave_sg: sg(%d) data length is zero\n", i);
  718. goto err;
  719. }
  720. mem_width = 2;
  721. if (unlikely(mem & 3 || len & 3))
  722. mem_width = 0;
  723. desc->lli.saddr = reg;
  724. desc->lli.daddr = mem;
  725. desc->lli.ctrla = ctrla
  726. | ATC_DST_WIDTH(mem_width)
  727. | len >> reg_width;
  728. desc->lli.ctrlb = ctrlb;
  729. atc_desc_chain(&first, &prev, desc);
  730. total_len += len;
  731. }
  732. break;
  733. default:
  734. return NULL;
  735. }
  736. /* set end-of-link to the last link descriptor of list*/
  737. set_desc_eol(prev);
  738. /* First descriptor of the chain embedds additional information */
  739. first->txd.cookie = -EBUSY;
  740. first->len = total_len;
  741. first->tx_width = reg_width;
  742. /* first link descriptor of list is responsible of flags */
  743. first->txd.flags = flags; /* client is in control of this ack */
  744. return &first->txd;
  745. err_desc_get:
  746. dev_err(chan2dev(chan), "not enough descriptors available\n");
  747. err:
  748. atc_desc_put(atchan, first);
  749. return NULL;
  750. }
  751. /**
  752. * atc_dma_cyclic_check_values
  753. * Check for too big/unaligned periods and unaligned DMA buffer
  754. */
  755. static int
  756. atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr,
  757. size_t period_len)
  758. {
  759. if (period_len > (ATC_BTSIZE_MAX << reg_width))
  760. goto err_out;
  761. if (unlikely(period_len & ((1 << reg_width) - 1)))
  762. goto err_out;
  763. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  764. goto err_out;
  765. return 0;
  766. err_out:
  767. return -EINVAL;
  768. }
  769. /**
  770. * atc_dma_cyclic_fill_desc - Fill one period descriptor
  771. */
  772. static int
  773. atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc,
  774. unsigned int period_index, dma_addr_t buf_addr,
  775. unsigned int reg_width, size_t period_len,
  776. enum dma_transfer_direction direction)
  777. {
  778. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  779. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  780. u32 ctrla;
  781. /* prepare common CRTLA value */
  782. ctrla = ATC_SCSIZE(sconfig->src_maxburst)
  783. | ATC_DCSIZE(sconfig->dst_maxburst)
  784. | ATC_DST_WIDTH(reg_width)
  785. | ATC_SRC_WIDTH(reg_width)
  786. | period_len >> reg_width;
  787. switch (direction) {
  788. case DMA_MEM_TO_DEV:
  789. desc->lli.saddr = buf_addr + (period_len * period_index);
  790. desc->lli.daddr = sconfig->dst_addr;
  791. desc->lli.ctrla = ctrla;
  792. desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED
  793. | ATC_SRC_ADDR_MODE_INCR
  794. | ATC_FC_MEM2PER
  795. | ATC_SIF(atchan->mem_if)
  796. | ATC_DIF(atchan->per_if);
  797. break;
  798. case DMA_DEV_TO_MEM:
  799. desc->lli.saddr = sconfig->src_addr;
  800. desc->lli.daddr = buf_addr + (period_len * period_index);
  801. desc->lli.ctrla = ctrla;
  802. desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR
  803. | ATC_SRC_ADDR_MODE_FIXED
  804. | ATC_FC_PER2MEM
  805. | ATC_SIF(atchan->per_if)
  806. | ATC_DIF(atchan->mem_if);
  807. break;
  808. default:
  809. return -EINVAL;
  810. }
  811. return 0;
  812. }
  813. /**
  814. * atc_prep_dma_cyclic - prepare the cyclic DMA transfer
  815. * @chan: the DMA channel to prepare
  816. * @buf_addr: physical DMA address where the buffer starts
  817. * @buf_len: total number of bytes for the entire buffer
  818. * @period_len: number of bytes for each period
  819. * @direction: transfer direction, to or from device
  820. * @flags: tx descriptor status flags
  821. * @context: transfer context (ignored)
  822. */
  823. static struct dma_async_tx_descriptor *
  824. atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
  825. size_t period_len, enum dma_transfer_direction direction,
  826. unsigned long flags, void *context)
  827. {
  828. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  829. struct at_dma_slave *atslave = chan->private;
  830. struct dma_slave_config *sconfig = &atchan->dma_sconfig;
  831. struct at_desc *first = NULL;
  832. struct at_desc *prev = NULL;
  833. unsigned long was_cyclic;
  834. unsigned int reg_width;
  835. unsigned int periods = buf_len / period_len;
  836. unsigned int i;
  837. dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n",
  838. direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE",
  839. buf_addr,
  840. periods, buf_len, period_len);
  841. if (unlikely(!atslave || !buf_len || !period_len)) {
  842. dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n");
  843. return NULL;
  844. }
  845. was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status);
  846. if (was_cyclic) {
  847. dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n");
  848. return NULL;
  849. }
  850. if (unlikely(!is_slave_direction(direction)))
  851. goto err_out;
  852. if (sconfig->direction == DMA_MEM_TO_DEV)
  853. reg_width = convert_buswidth(sconfig->dst_addr_width);
  854. else
  855. reg_width = convert_buswidth(sconfig->src_addr_width);
  856. /* Check for too big/unaligned periods and unaligned DMA buffer */
  857. if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len))
  858. goto err_out;
  859. /* build cyclic linked list */
  860. for (i = 0; i < periods; i++) {
  861. struct at_desc *desc;
  862. desc = atc_desc_get(atchan);
  863. if (!desc)
  864. goto err_desc_get;
  865. if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr,
  866. reg_width, period_len, direction))
  867. goto err_desc_get;
  868. atc_desc_chain(&first, &prev, desc);
  869. }
  870. /* lets make a cyclic list */
  871. prev->lli.dscr = first->txd.phys;
  872. /* First descriptor of the chain embedds additional information */
  873. first->txd.cookie = -EBUSY;
  874. first->len = buf_len;
  875. first->tx_width = reg_width;
  876. return &first->txd;
  877. err_desc_get:
  878. dev_err(chan2dev(chan), "not enough descriptors available\n");
  879. atc_desc_put(atchan, first);
  880. err_out:
  881. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  882. return NULL;
  883. }
  884. static int set_runtime_config(struct dma_chan *chan,
  885. struct dma_slave_config *sconfig)
  886. {
  887. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  888. /* Check if it is chan is configured for slave transfers */
  889. if (!chan->private)
  890. return -EINVAL;
  891. memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig));
  892. convert_burst(&atchan->dma_sconfig.src_maxburst);
  893. convert_burst(&atchan->dma_sconfig.dst_maxburst);
  894. return 0;
  895. }
  896. static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  897. unsigned long arg)
  898. {
  899. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  900. struct at_dma *atdma = to_at_dma(chan->device);
  901. int chan_id = atchan->chan_common.chan_id;
  902. unsigned long flags;
  903. LIST_HEAD(list);
  904. dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd);
  905. if (cmd == DMA_PAUSE) {
  906. spin_lock_irqsave(&atchan->lock, flags);
  907. dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id));
  908. set_bit(ATC_IS_PAUSED, &atchan->status);
  909. spin_unlock_irqrestore(&atchan->lock, flags);
  910. } else if (cmd == DMA_RESUME) {
  911. if (!atc_chan_is_paused(atchan))
  912. return 0;
  913. spin_lock_irqsave(&atchan->lock, flags);
  914. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id));
  915. clear_bit(ATC_IS_PAUSED, &atchan->status);
  916. spin_unlock_irqrestore(&atchan->lock, flags);
  917. } else if (cmd == DMA_TERMINATE_ALL) {
  918. struct at_desc *desc, *_desc;
  919. /*
  920. * This is only called when something went wrong elsewhere, so
  921. * we don't really care about the data. Just disable the
  922. * channel. We still have to poll the channel enable bit due
  923. * to AHB/HSB limitations.
  924. */
  925. spin_lock_irqsave(&atchan->lock, flags);
  926. /* disabling channel: must also remove suspend state */
  927. dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask);
  928. /* confirm that this channel is disabled */
  929. while (dma_readl(atdma, CHSR) & atchan->mask)
  930. cpu_relax();
  931. /* active_list entries will end up before queued entries */
  932. list_splice_init(&atchan->queue, &list);
  933. list_splice_init(&atchan->active_list, &list);
  934. /* Flush all pending and queued descriptors */
  935. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  936. atc_chain_complete(atchan, desc);
  937. clear_bit(ATC_IS_PAUSED, &atchan->status);
  938. /* if channel dedicated to cyclic operations, free it */
  939. clear_bit(ATC_IS_CYCLIC, &atchan->status);
  940. spin_unlock_irqrestore(&atchan->lock, flags);
  941. } else if (cmd == DMA_SLAVE_CONFIG) {
  942. return set_runtime_config(chan, (struct dma_slave_config *)arg);
  943. } else {
  944. return -ENXIO;
  945. }
  946. return 0;
  947. }
  948. /**
  949. * atc_tx_status - poll for transaction completion
  950. * @chan: DMA channel
  951. * @cookie: transaction identifier to check status of
  952. * @txstate: if not %NULL updated with transaction state
  953. *
  954. * If @txstate is passed in, upon return it reflect the driver
  955. * internal state and can be used with dma_async_is_complete() to check
  956. * the status of multiple cookies without re-checking hardware state.
  957. */
  958. static enum dma_status
  959. atc_tx_status(struct dma_chan *chan,
  960. dma_cookie_t cookie,
  961. struct dma_tx_state *txstate)
  962. {
  963. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  964. unsigned long flags;
  965. enum dma_status ret;
  966. int bytes = 0;
  967. ret = dma_cookie_status(chan, cookie, txstate);
  968. if (ret == DMA_SUCCESS)
  969. return ret;
  970. /*
  971. * There's no point calculating the residue if there's
  972. * no txstate to store the value.
  973. */
  974. if (!txstate)
  975. return DMA_ERROR;
  976. spin_lock_irqsave(&atchan->lock, flags);
  977. /* Get number of bytes left in the active transactions */
  978. bytes = atc_get_bytes_left(chan);
  979. spin_unlock_irqrestore(&atchan->lock, flags);
  980. if (unlikely(bytes < 0)) {
  981. dev_vdbg(chan2dev(chan), "get residual bytes error\n");
  982. return DMA_ERROR;
  983. } else
  984. dma_set_residue(txstate, bytes);
  985. dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n",
  986. ret, cookie, bytes);
  987. return ret;
  988. }
  989. /**
  990. * atc_issue_pending - try to finish work
  991. * @chan: target DMA channel
  992. */
  993. static void atc_issue_pending(struct dma_chan *chan)
  994. {
  995. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  996. unsigned long flags;
  997. dev_vdbg(chan2dev(chan), "issue_pending\n");
  998. /* Not needed for cyclic transfers */
  999. if (atc_chan_is_cyclic(atchan))
  1000. return;
  1001. spin_lock_irqsave(&atchan->lock, flags);
  1002. atc_advance_work(atchan);
  1003. spin_unlock_irqrestore(&atchan->lock, flags);
  1004. }
  1005. /**
  1006. * atc_alloc_chan_resources - allocate resources for DMA channel
  1007. * @chan: allocate descriptor resources for this channel
  1008. * @client: current client requesting the channel be ready for requests
  1009. *
  1010. * return - the number of allocated descriptors
  1011. */
  1012. static int atc_alloc_chan_resources(struct dma_chan *chan)
  1013. {
  1014. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1015. struct at_dma *atdma = to_at_dma(chan->device);
  1016. struct at_desc *desc;
  1017. struct at_dma_slave *atslave;
  1018. unsigned long flags;
  1019. int i;
  1020. u32 cfg;
  1021. LIST_HEAD(tmp_list);
  1022. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  1023. /* ASSERT: channel is idle */
  1024. if (atc_chan_is_enabled(atchan)) {
  1025. dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
  1026. return -EIO;
  1027. }
  1028. cfg = ATC_DEFAULT_CFG;
  1029. atslave = chan->private;
  1030. if (atslave) {
  1031. /*
  1032. * We need controller-specific data to set up slave
  1033. * transfers.
  1034. */
  1035. BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
  1036. /* if cfg configuration specified take it instead of default */
  1037. if (atslave->cfg)
  1038. cfg = atslave->cfg;
  1039. }
  1040. /* have we already been set up?
  1041. * reconfigure channel but no need to reallocate descriptors */
  1042. if (!list_empty(&atchan->free_list))
  1043. return atchan->descs_allocated;
  1044. /* Allocate initial pool of descriptors */
  1045. for (i = 0; i < init_nr_desc_per_channel; i++) {
  1046. desc = atc_alloc_descriptor(chan, GFP_KERNEL);
  1047. if (!desc) {
  1048. dev_err(atdma->dma_common.dev,
  1049. "Only %d initial descriptors\n", i);
  1050. break;
  1051. }
  1052. list_add_tail(&desc->desc_node, &tmp_list);
  1053. }
  1054. spin_lock_irqsave(&atchan->lock, flags);
  1055. atchan->descs_allocated = i;
  1056. atchan->remain_desc = 0;
  1057. list_splice(&tmp_list, &atchan->free_list);
  1058. dma_cookie_init(chan);
  1059. spin_unlock_irqrestore(&atchan->lock, flags);
  1060. /* channel parameters */
  1061. channel_writel(atchan, CFG, cfg);
  1062. dev_dbg(chan2dev(chan),
  1063. "alloc_chan_resources: allocated %d descriptors\n",
  1064. atchan->descs_allocated);
  1065. return atchan->descs_allocated;
  1066. }
  1067. /**
  1068. * atc_free_chan_resources - free all channel resources
  1069. * @chan: DMA channel
  1070. */
  1071. static void atc_free_chan_resources(struct dma_chan *chan)
  1072. {
  1073. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1074. struct at_dma *atdma = to_at_dma(chan->device);
  1075. struct at_desc *desc, *_desc;
  1076. LIST_HEAD(list);
  1077. dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
  1078. atchan->descs_allocated);
  1079. /* ASSERT: channel is idle */
  1080. BUG_ON(!list_empty(&atchan->active_list));
  1081. BUG_ON(!list_empty(&atchan->queue));
  1082. BUG_ON(atc_chan_is_enabled(atchan));
  1083. list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
  1084. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  1085. list_del(&desc->desc_node);
  1086. /* free link descriptor */
  1087. dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
  1088. }
  1089. list_splice_init(&atchan->free_list, &list);
  1090. atchan->descs_allocated = 0;
  1091. atchan->status = 0;
  1092. atchan->remain_desc = 0;
  1093. dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
  1094. }
  1095. #ifdef CONFIG_OF
  1096. static bool at_dma_filter(struct dma_chan *chan, void *slave)
  1097. {
  1098. struct at_dma_slave *atslave = slave;
  1099. if (atslave->dma_dev == chan->device->dev) {
  1100. chan->private = atslave;
  1101. return true;
  1102. } else {
  1103. return false;
  1104. }
  1105. }
  1106. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1107. struct of_dma *of_dma)
  1108. {
  1109. struct dma_chan *chan;
  1110. struct at_dma_chan *atchan;
  1111. struct at_dma_slave *atslave;
  1112. dma_cap_mask_t mask;
  1113. unsigned int per_id;
  1114. struct platform_device *dmac_pdev;
  1115. if (dma_spec->args_count != 2)
  1116. return NULL;
  1117. dmac_pdev = of_find_device_by_node(dma_spec->np);
  1118. dma_cap_zero(mask);
  1119. dma_cap_set(DMA_SLAVE, mask);
  1120. atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL);
  1121. if (!atslave)
  1122. return NULL;
  1123. /*
  1124. * We can fill both SRC_PER and DST_PER, one of these fields will be
  1125. * ignored depending on DMA transfer direction.
  1126. */
  1127. per_id = dma_spec->args[1];
  1128. atslave->cfg = ATC_FIFOCFG_HALFFIFO
  1129. | ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW
  1130. | ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id)
  1131. | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id);
  1132. atslave->dma_dev = &dmac_pdev->dev;
  1133. chan = dma_request_channel(mask, at_dma_filter, atslave);
  1134. if (!chan)
  1135. return NULL;
  1136. atchan = to_at_dma_chan(chan);
  1137. atchan->per_if = dma_spec->args[0] & 0xff;
  1138. atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff;
  1139. return chan;
  1140. }
  1141. #else
  1142. static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec,
  1143. struct of_dma *of_dma)
  1144. {
  1145. return NULL;
  1146. }
  1147. #endif
  1148. /*-- Module Management -----------------------------------------------*/
  1149. /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */
  1150. static struct at_dma_platform_data at91sam9rl_config = {
  1151. .nr_channels = 2,
  1152. };
  1153. static struct at_dma_platform_data at91sam9g45_config = {
  1154. .nr_channels = 8,
  1155. };
  1156. #if defined(CONFIG_OF)
  1157. static const struct of_device_id atmel_dma_dt_ids[] = {
  1158. {
  1159. .compatible = "atmel,at91sam9rl-dma",
  1160. .data = &at91sam9rl_config,
  1161. }, {
  1162. .compatible = "atmel,at91sam9g45-dma",
  1163. .data = &at91sam9g45_config,
  1164. }, {
  1165. /* sentinel */
  1166. }
  1167. };
  1168. MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids);
  1169. #endif
  1170. static const struct platform_device_id atdma_devtypes[] = {
  1171. {
  1172. .name = "at91sam9rl_dma",
  1173. .driver_data = (unsigned long) &at91sam9rl_config,
  1174. }, {
  1175. .name = "at91sam9g45_dma",
  1176. .driver_data = (unsigned long) &at91sam9g45_config,
  1177. }, {
  1178. /* sentinel */
  1179. }
  1180. };
  1181. static inline const struct at_dma_platform_data * __init at_dma_get_driver_data(
  1182. struct platform_device *pdev)
  1183. {
  1184. if (pdev->dev.of_node) {
  1185. const struct of_device_id *match;
  1186. match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node);
  1187. if (match == NULL)
  1188. return NULL;
  1189. return match->data;
  1190. }
  1191. return (struct at_dma_platform_data *)
  1192. platform_get_device_id(pdev)->driver_data;
  1193. }
  1194. /**
  1195. * at_dma_off - disable DMA controller
  1196. * @atdma: the Atmel HDAMC device
  1197. */
  1198. static void at_dma_off(struct at_dma *atdma)
  1199. {
  1200. dma_writel(atdma, EN, 0);
  1201. /* disable all interrupts */
  1202. dma_writel(atdma, EBCIDR, -1L);
  1203. /* confirm that all channels are disabled */
  1204. while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
  1205. cpu_relax();
  1206. }
  1207. static int __init at_dma_probe(struct platform_device *pdev)
  1208. {
  1209. struct resource *io;
  1210. struct at_dma *atdma;
  1211. size_t size;
  1212. int irq;
  1213. int err;
  1214. int i;
  1215. const struct at_dma_platform_data *plat_dat;
  1216. /* setup platform data for each SoC */
  1217. dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
  1218. dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask);
  1219. dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask);
  1220. /* get DMA parameters from controller type */
  1221. plat_dat = at_dma_get_driver_data(pdev);
  1222. if (!plat_dat)
  1223. return -ENODEV;
  1224. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1225. if (!io)
  1226. return -EINVAL;
  1227. irq = platform_get_irq(pdev, 0);
  1228. if (irq < 0)
  1229. return irq;
  1230. size = sizeof(struct at_dma);
  1231. size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
  1232. atdma = kzalloc(size, GFP_KERNEL);
  1233. if (!atdma)
  1234. return -ENOMEM;
  1235. /* discover transaction capabilities */
  1236. atdma->dma_common.cap_mask = plat_dat->cap_mask;
  1237. atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
  1238. size = resource_size(io);
  1239. if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
  1240. err = -EBUSY;
  1241. goto err_kfree;
  1242. }
  1243. atdma->regs = ioremap(io->start, size);
  1244. if (!atdma->regs) {
  1245. err = -ENOMEM;
  1246. goto err_release_r;
  1247. }
  1248. atdma->clk = clk_get(&pdev->dev, "dma_clk");
  1249. if (IS_ERR(atdma->clk)) {
  1250. err = PTR_ERR(atdma->clk);
  1251. goto err_clk;
  1252. }
  1253. clk_enable(atdma->clk);
  1254. /* force dma off, just in case */
  1255. at_dma_off(atdma);
  1256. err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
  1257. if (err)
  1258. goto err_irq;
  1259. platform_set_drvdata(pdev, atdma);
  1260. /* create a pool of consistent memory blocks for hardware descriptors */
  1261. atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
  1262. &pdev->dev, sizeof(struct at_desc),
  1263. 4 /* word alignment */, 0);
  1264. if (!atdma->dma_desc_pool) {
  1265. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1266. err = -ENOMEM;
  1267. goto err_pool_create;
  1268. }
  1269. /* clear any pending interrupt */
  1270. while (dma_readl(atdma, EBCISR))
  1271. cpu_relax();
  1272. /* initialize channels related values */
  1273. INIT_LIST_HEAD(&atdma->dma_common.channels);
  1274. for (i = 0; i < plat_dat->nr_channels; i++) {
  1275. struct at_dma_chan *atchan = &atdma->chan[i];
  1276. atchan->mem_if = AT_DMA_MEM_IF;
  1277. atchan->per_if = AT_DMA_PER_IF;
  1278. atchan->chan_common.device = &atdma->dma_common;
  1279. dma_cookie_init(&atchan->chan_common);
  1280. list_add_tail(&atchan->chan_common.device_node,
  1281. &atdma->dma_common.channels);
  1282. atchan->ch_regs = atdma->regs + ch_regs(i);
  1283. spin_lock_init(&atchan->lock);
  1284. atchan->mask = 1 << i;
  1285. INIT_LIST_HEAD(&atchan->active_list);
  1286. INIT_LIST_HEAD(&atchan->queue);
  1287. INIT_LIST_HEAD(&atchan->free_list);
  1288. tasklet_init(&atchan->tasklet, atc_tasklet,
  1289. (unsigned long)atchan);
  1290. atc_enable_chan_irq(atdma, i);
  1291. }
  1292. /* set base routines */
  1293. atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
  1294. atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
  1295. atdma->dma_common.device_tx_status = atc_tx_status;
  1296. atdma->dma_common.device_issue_pending = atc_issue_pending;
  1297. atdma->dma_common.dev = &pdev->dev;
  1298. /* set prep routines based on capability */
  1299. if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
  1300. atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
  1301. if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
  1302. atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
  1303. /* controller can do slave DMA: can trigger cyclic transfers */
  1304. dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask);
  1305. atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic;
  1306. atdma->dma_common.device_control = atc_control;
  1307. }
  1308. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1309. dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
  1310. dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
  1311. dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "",
  1312. plat_dat->nr_channels);
  1313. dma_async_device_register(&atdma->dma_common);
  1314. /*
  1315. * Do not return an error if the dmac node is not present in order to
  1316. * not break the existing way of requesting channel with
  1317. * dma_request_channel().
  1318. */
  1319. if (pdev->dev.of_node) {
  1320. err = of_dma_controller_register(pdev->dev.of_node,
  1321. at_dma_xlate, atdma);
  1322. if (err) {
  1323. dev_err(&pdev->dev, "could not register of_dma_controller\n");
  1324. goto err_of_dma_controller_register;
  1325. }
  1326. }
  1327. return 0;
  1328. err_of_dma_controller_register:
  1329. dma_async_device_unregister(&atdma->dma_common);
  1330. dma_pool_destroy(atdma->dma_desc_pool);
  1331. err_pool_create:
  1332. platform_set_drvdata(pdev, NULL);
  1333. free_irq(platform_get_irq(pdev, 0), atdma);
  1334. err_irq:
  1335. clk_disable(atdma->clk);
  1336. clk_put(atdma->clk);
  1337. err_clk:
  1338. iounmap(atdma->regs);
  1339. atdma->regs = NULL;
  1340. err_release_r:
  1341. release_mem_region(io->start, size);
  1342. err_kfree:
  1343. kfree(atdma);
  1344. return err;
  1345. }
  1346. static int at_dma_remove(struct platform_device *pdev)
  1347. {
  1348. struct at_dma *atdma = platform_get_drvdata(pdev);
  1349. struct dma_chan *chan, *_chan;
  1350. struct resource *io;
  1351. at_dma_off(atdma);
  1352. dma_async_device_unregister(&atdma->dma_common);
  1353. dma_pool_destroy(atdma->dma_desc_pool);
  1354. platform_set_drvdata(pdev, NULL);
  1355. free_irq(platform_get_irq(pdev, 0), atdma);
  1356. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1357. device_node) {
  1358. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1359. /* Disable interrupts */
  1360. atc_disable_chan_irq(atdma, chan->chan_id);
  1361. tasklet_disable(&atchan->tasklet);
  1362. tasklet_kill(&atchan->tasklet);
  1363. list_del(&chan->device_node);
  1364. }
  1365. clk_disable(atdma->clk);
  1366. clk_put(atdma->clk);
  1367. iounmap(atdma->regs);
  1368. atdma->regs = NULL;
  1369. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1370. release_mem_region(io->start, resource_size(io));
  1371. kfree(atdma);
  1372. return 0;
  1373. }
  1374. static void at_dma_shutdown(struct platform_device *pdev)
  1375. {
  1376. struct at_dma *atdma = platform_get_drvdata(pdev);
  1377. at_dma_off(platform_get_drvdata(pdev));
  1378. clk_disable(atdma->clk);
  1379. }
  1380. static int at_dma_prepare(struct device *dev)
  1381. {
  1382. struct platform_device *pdev = to_platform_device(dev);
  1383. struct at_dma *atdma = platform_get_drvdata(pdev);
  1384. struct dma_chan *chan, *_chan;
  1385. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1386. device_node) {
  1387. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1388. /* wait for transaction completion (except in cyclic case) */
  1389. if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan))
  1390. return -EAGAIN;
  1391. }
  1392. return 0;
  1393. }
  1394. static void atc_suspend_cyclic(struct at_dma_chan *atchan)
  1395. {
  1396. struct dma_chan *chan = &atchan->chan_common;
  1397. /* Channel should be paused by user
  1398. * do it anyway even if it is not done already */
  1399. if (!atc_chan_is_paused(atchan)) {
  1400. dev_warn(chan2dev(chan),
  1401. "cyclic channel not paused, should be done by channel user\n");
  1402. atc_control(chan, DMA_PAUSE, 0);
  1403. }
  1404. /* now preserve additional data for cyclic operations */
  1405. /* next descriptor address in the cyclic list */
  1406. atchan->save_dscr = channel_readl(atchan, DSCR);
  1407. vdbg_dump_regs(atchan);
  1408. }
  1409. static int at_dma_suspend_noirq(struct device *dev)
  1410. {
  1411. struct platform_device *pdev = to_platform_device(dev);
  1412. struct at_dma *atdma = platform_get_drvdata(pdev);
  1413. struct dma_chan *chan, *_chan;
  1414. /* preserve data */
  1415. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1416. device_node) {
  1417. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1418. if (atc_chan_is_cyclic(atchan))
  1419. atc_suspend_cyclic(atchan);
  1420. atchan->save_cfg = channel_readl(atchan, CFG);
  1421. }
  1422. atdma->save_imr = dma_readl(atdma, EBCIMR);
  1423. /* disable DMA controller */
  1424. at_dma_off(atdma);
  1425. clk_disable(atdma->clk);
  1426. return 0;
  1427. }
  1428. static void atc_resume_cyclic(struct at_dma_chan *atchan)
  1429. {
  1430. struct at_dma *atdma = to_at_dma(atchan->chan_common.device);
  1431. /* restore channel status for cyclic descriptors list:
  1432. * next descriptor in the cyclic list at the time of suspend */
  1433. channel_writel(atchan, SADDR, 0);
  1434. channel_writel(atchan, DADDR, 0);
  1435. channel_writel(atchan, CTRLA, 0);
  1436. channel_writel(atchan, CTRLB, 0);
  1437. channel_writel(atchan, DSCR, atchan->save_dscr);
  1438. dma_writel(atdma, CHER, atchan->mask);
  1439. /* channel pause status should be removed by channel user
  1440. * We cannot take the initiative to do it here */
  1441. vdbg_dump_regs(atchan);
  1442. }
  1443. static int at_dma_resume_noirq(struct device *dev)
  1444. {
  1445. struct platform_device *pdev = to_platform_device(dev);
  1446. struct at_dma *atdma = platform_get_drvdata(pdev);
  1447. struct dma_chan *chan, *_chan;
  1448. /* bring back DMA controller */
  1449. clk_enable(atdma->clk);
  1450. dma_writel(atdma, EN, AT_DMA_ENABLE);
  1451. /* clear any pending interrupt */
  1452. while (dma_readl(atdma, EBCISR))
  1453. cpu_relax();
  1454. /* restore saved data */
  1455. dma_writel(atdma, EBCIER, atdma->save_imr);
  1456. list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
  1457. device_node) {
  1458. struct at_dma_chan *atchan = to_at_dma_chan(chan);
  1459. channel_writel(atchan, CFG, atchan->save_cfg);
  1460. if (atc_chan_is_cyclic(atchan))
  1461. atc_resume_cyclic(atchan);
  1462. }
  1463. return 0;
  1464. }
  1465. static const struct dev_pm_ops at_dma_dev_pm_ops = {
  1466. .prepare = at_dma_prepare,
  1467. .suspend_noirq = at_dma_suspend_noirq,
  1468. .resume_noirq = at_dma_resume_noirq,
  1469. };
  1470. static struct platform_driver at_dma_driver = {
  1471. .remove = at_dma_remove,
  1472. .shutdown = at_dma_shutdown,
  1473. .id_table = atdma_devtypes,
  1474. .driver = {
  1475. .name = "at_hdmac",
  1476. .pm = &at_dma_dev_pm_ops,
  1477. .of_match_table = of_match_ptr(atmel_dma_dt_ids),
  1478. },
  1479. };
  1480. static int __init at_dma_init(void)
  1481. {
  1482. return platform_driver_probe(&at_dma_driver, at_dma_probe);
  1483. }
  1484. subsys_initcall(at_dma_init);
  1485. static void __exit at_dma_exit(void)
  1486. {
  1487. platform_driver_unregister(&at_dma_driver);
  1488. }
  1489. module_exit(at_dma_exit);
  1490. MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
  1491. MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
  1492. MODULE_LICENSE("GPL");
  1493. MODULE_ALIAS("platform:at_hdmac");