at_hdmac.c 46 KB

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