at_hdmac.c 52 KB

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