core.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748
  1. /*
  2. * Core driver for the Synopsys DesignWare DMA Controller
  3. *
  4. * Copyright (C) 2007-2008 Atmel Corporation
  5. * Copyright (C) 2010-2011 ST Microelectronics
  6. * Copyright (C) 2013 Intel Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/bitops.h>
  13. #include <linux/delay.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/dmapool.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/pm_runtime.h>
  25. #include "../dmaengine.h"
  26. #include "internal.h"
  27. /*
  28. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  29. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  30. * of which use ARM any more). See the "Databook" from Synopsys for
  31. * information beyond what licensees probably provide.
  32. *
  33. * The driver has been tested with the Atmel AT32AP7000, which does not
  34. * support descriptor writeback.
  35. */
  36. #define DWC_DEFAULT_CTLLO(_chan) ({ \
  37. struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
  38. struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
  39. bool _is_slave = is_slave_direction(_dwc->direction); \
  40. u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
  41. DW_DMA_MSIZE_16; \
  42. u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
  43. DW_DMA_MSIZE_16; \
  44. \
  45. (DWC_CTLL_DST_MSIZE(_dmsize) \
  46. | DWC_CTLL_SRC_MSIZE(_smsize) \
  47. | DWC_CTLL_LLP_D_EN \
  48. | DWC_CTLL_LLP_S_EN \
  49. | DWC_CTLL_DMS(_dwc->dst_master) \
  50. | DWC_CTLL_SMS(_dwc->src_master)); \
  51. })
  52. /*
  53. * Number of descriptors to allocate for each channel. This should be
  54. * made configurable somehow; preferably, the clients (at least the
  55. * ones using slave transfers) should be able to give us a hint.
  56. */
  57. #define NR_DESCS_PER_CHANNEL 64
  58. /* The set of bus widths supported by the DMA controller */
  59. #define DW_DMA_BUSWIDTHS \
  60. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  61. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  62. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  63. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
  64. /*----------------------------------------------------------------------*/
  65. static struct device *chan2dev(struct dma_chan *chan)
  66. {
  67. return &chan->dev->device;
  68. }
  69. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  70. {
  71. return to_dw_desc(dwc->active_list.next);
  72. }
  73. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  74. {
  75. struct dw_desc *desc, *_desc;
  76. struct dw_desc *ret = NULL;
  77. unsigned int i = 0;
  78. unsigned long flags;
  79. spin_lock_irqsave(&dwc->lock, flags);
  80. list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
  81. i++;
  82. if (async_tx_test_ack(&desc->txd)) {
  83. list_del(&desc->desc_node);
  84. ret = desc;
  85. break;
  86. }
  87. dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
  88. }
  89. spin_unlock_irqrestore(&dwc->lock, flags);
  90. dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
  91. return ret;
  92. }
  93. /*
  94. * Move a descriptor, including any children, to the free list.
  95. * `desc' must not be on any lists.
  96. */
  97. static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
  98. {
  99. unsigned long flags;
  100. if (desc) {
  101. struct dw_desc *child;
  102. spin_lock_irqsave(&dwc->lock, flags);
  103. list_for_each_entry(child, &desc->tx_list, desc_node)
  104. dev_vdbg(chan2dev(&dwc->chan),
  105. "moving child desc %p to freelist\n",
  106. child);
  107. list_splice_init(&desc->tx_list, &dwc->free_list);
  108. dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
  109. list_add(&desc->desc_node, &dwc->free_list);
  110. spin_unlock_irqrestore(&dwc->lock, flags);
  111. }
  112. }
  113. static void dwc_initialize(struct dw_dma_chan *dwc)
  114. {
  115. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  116. struct dw_dma_slave *dws = dwc->chan.private;
  117. u32 cfghi = DWC_CFGH_FIFO_MODE;
  118. u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
  119. if (dwc->initialized == true)
  120. return;
  121. if (dws) {
  122. /*
  123. * We need controller-specific data to set up slave
  124. * transfers.
  125. */
  126. BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
  127. cfghi |= DWC_CFGH_DST_PER(dws->dst_id);
  128. cfghi |= DWC_CFGH_SRC_PER(dws->src_id);
  129. } else {
  130. cfghi |= DWC_CFGH_DST_PER(dwc->dst_id);
  131. cfghi |= DWC_CFGH_SRC_PER(dwc->src_id);
  132. }
  133. channel_writel(dwc, CFG_LO, cfglo);
  134. channel_writel(dwc, CFG_HI, cfghi);
  135. /* Enable interrupts */
  136. channel_set_bit(dw, MASK.XFER, dwc->mask);
  137. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  138. dwc->initialized = true;
  139. }
  140. /*----------------------------------------------------------------------*/
  141. static inline unsigned int dwc_fast_ffs(unsigned long long v)
  142. {
  143. /*
  144. * We can be a lot more clever here, but this should take care
  145. * of the most common optimization.
  146. */
  147. if (!(v & 7))
  148. return 3;
  149. else if (!(v & 3))
  150. return 2;
  151. else if (!(v & 1))
  152. return 1;
  153. return 0;
  154. }
  155. static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
  156. {
  157. dev_err(chan2dev(&dwc->chan),
  158. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  159. channel_readl(dwc, SAR),
  160. channel_readl(dwc, DAR),
  161. channel_readl(dwc, LLP),
  162. channel_readl(dwc, CTL_HI),
  163. channel_readl(dwc, CTL_LO));
  164. }
  165. static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
  166. {
  167. channel_clear_bit(dw, CH_EN, dwc->mask);
  168. while (dma_readl(dw, CH_EN) & dwc->mask)
  169. cpu_relax();
  170. }
  171. /*----------------------------------------------------------------------*/
  172. /* Perform single block transfer */
  173. static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
  174. struct dw_desc *desc)
  175. {
  176. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  177. u32 ctllo;
  178. /*
  179. * Software emulation of LLP mode relies on interrupts to continue
  180. * multi block transfer.
  181. */
  182. ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
  183. channel_writel(dwc, SAR, desc->lli.sar);
  184. channel_writel(dwc, DAR, desc->lli.dar);
  185. channel_writel(dwc, CTL_LO, ctllo);
  186. channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
  187. channel_set_bit(dw, CH_EN, dwc->mask);
  188. /* Move pointer to next descriptor */
  189. dwc->tx_node_active = dwc->tx_node_active->next;
  190. }
  191. /* Called with dwc->lock held and bh disabled */
  192. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  193. {
  194. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  195. unsigned long was_soft_llp;
  196. /* ASSERT: channel is idle */
  197. if (dma_readl(dw, CH_EN) & dwc->mask) {
  198. dev_err(chan2dev(&dwc->chan),
  199. "%s: BUG: Attempted to start non-idle channel\n",
  200. __func__);
  201. dwc_dump_chan_regs(dwc);
  202. /* The tasklet will hopefully advance the queue... */
  203. return;
  204. }
  205. if (dwc->nollp) {
  206. was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
  207. &dwc->flags);
  208. if (was_soft_llp) {
  209. dev_err(chan2dev(&dwc->chan),
  210. "BUG: Attempted to start new LLP transfer inside ongoing one\n");
  211. return;
  212. }
  213. dwc_initialize(dwc);
  214. dwc->residue = first->total_len;
  215. dwc->tx_node_active = &first->tx_list;
  216. /* Submit first block */
  217. dwc_do_single_block(dwc, first);
  218. return;
  219. }
  220. dwc_initialize(dwc);
  221. channel_writel(dwc, LLP, first->txd.phys);
  222. channel_writel(dwc, CTL_LO,
  223. DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  224. channel_writel(dwc, CTL_HI, 0);
  225. channel_set_bit(dw, CH_EN, dwc->mask);
  226. }
  227. static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
  228. {
  229. struct dw_desc *desc;
  230. if (list_empty(&dwc->queue))
  231. return;
  232. list_move(dwc->queue.next, &dwc->active_list);
  233. desc = dwc_first_active(dwc);
  234. dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
  235. dwc_dostart(dwc, desc);
  236. }
  237. /*----------------------------------------------------------------------*/
  238. static void
  239. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
  240. bool callback_required)
  241. {
  242. dma_async_tx_callback callback = NULL;
  243. void *param = NULL;
  244. struct dma_async_tx_descriptor *txd = &desc->txd;
  245. struct dw_desc *child;
  246. unsigned long flags;
  247. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  248. spin_lock_irqsave(&dwc->lock, flags);
  249. dma_cookie_complete(txd);
  250. if (callback_required) {
  251. callback = txd->callback;
  252. param = txd->callback_param;
  253. }
  254. /* async_tx_ack */
  255. list_for_each_entry(child, &desc->tx_list, desc_node)
  256. async_tx_ack(&child->txd);
  257. async_tx_ack(&desc->txd);
  258. list_splice_init(&desc->tx_list, &dwc->free_list);
  259. list_move(&desc->desc_node, &dwc->free_list);
  260. dma_descriptor_unmap(txd);
  261. spin_unlock_irqrestore(&dwc->lock, flags);
  262. if (callback)
  263. callback(param);
  264. }
  265. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  266. {
  267. struct dw_desc *desc, *_desc;
  268. LIST_HEAD(list);
  269. unsigned long flags;
  270. spin_lock_irqsave(&dwc->lock, flags);
  271. if (dma_readl(dw, CH_EN) & dwc->mask) {
  272. dev_err(chan2dev(&dwc->chan),
  273. "BUG: XFER bit set, but channel not idle!\n");
  274. /* Try to continue after resetting the channel... */
  275. dwc_chan_disable(dw, dwc);
  276. }
  277. /*
  278. * Submit queued descriptors ASAP, i.e. before we go through
  279. * the completed ones.
  280. */
  281. list_splice_init(&dwc->active_list, &list);
  282. dwc_dostart_first_queued(dwc);
  283. spin_unlock_irqrestore(&dwc->lock, flags);
  284. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  285. dwc_descriptor_complete(dwc, desc, true);
  286. }
  287. /* Returns how many bytes were already received from source */
  288. static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
  289. {
  290. u32 ctlhi = channel_readl(dwc, CTL_HI);
  291. u32 ctllo = channel_readl(dwc, CTL_LO);
  292. return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
  293. }
  294. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  295. {
  296. dma_addr_t llp;
  297. struct dw_desc *desc, *_desc;
  298. struct dw_desc *child;
  299. u32 status_xfer;
  300. unsigned long flags;
  301. spin_lock_irqsave(&dwc->lock, flags);
  302. llp = channel_readl(dwc, LLP);
  303. status_xfer = dma_readl(dw, RAW.XFER);
  304. if (status_xfer & dwc->mask) {
  305. /* Everything we've submitted is done */
  306. dma_writel(dw, CLEAR.XFER, dwc->mask);
  307. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  308. struct list_head *head, *active = dwc->tx_node_active;
  309. /*
  310. * We are inside first active descriptor.
  311. * Otherwise something is really wrong.
  312. */
  313. desc = dwc_first_active(dwc);
  314. head = &desc->tx_list;
  315. if (active != head) {
  316. /* Update desc to reflect last sent one */
  317. if (active != head->next)
  318. desc = to_dw_desc(active->prev);
  319. dwc->residue -= desc->len;
  320. child = to_dw_desc(active);
  321. /* Submit next block */
  322. dwc_do_single_block(dwc, child);
  323. spin_unlock_irqrestore(&dwc->lock, flags);
  324. return;
  325. }
  326. /* We are done here */
  327. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  328. }
  329. dwc->residue = 0;
  330. spin_unlock_irqrestore(&dwc->lock, flags);
  331. dwc_complete_all(dw, dwc);
  332. return;
  333. }
  334. if (list_empty(&dwc->active_list)) {
  335. dwc->residue = 0;
  336. spin_unlock_irqrestore(&dwc->lock, flags);
  337. return;
  338. }
  339. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  340. dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
  341. spin_unlock_irqrestore(&dwc->lock, flags);
  342. return;
  343. }
  344. dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
  345. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  346. /* Initial residue value */
  347. dwc->residue = desc->total_len;
  348. /* Check first descriptors addr */
  349. if (desc->txd.phys == llp) {
  350. spin_unlock_irqrestore(&dwc->lock, flags);
  351. return;
  352. }
  353. /* Check first descriptors llp */
  354. if (desc->lli.llp == llp) {
  355. /* This one is currently in progress */
  356. dwc->residue -= dwc_get_sent(dwc);
  357. spin_unlock_irqrestore(&dwc->lock, flags);
  358. return;
  359. }
  360. dwc->residue -= desc->len;
  361. list_for_each_entry(child, &desc->tx_list, desc_node) {
  362. if (child->lli.llp == llp) {
  363. /* Currently in progress */
  364. dwc->residue -= dwc_get_sent(dwc);
  365. spin_unlock_irqrestore(&dwc->lock, flags);
  366. return;
  367. }
  368. dwc->residue -= child->len;
  369. }
  370. /*
  371. * No descriptors so far seem to be in progress, i.e.
  372. * this one must be done.
  373. */
  374. spin_unlock_irqrestore(&dwc->lock, flags);
  375. dwc_descriptor_complete(dwc, desc, true);
  376. spin_lock_irqsave(&dwc->lock, flags);
  377. }
  378. dev_err(chan2dev(&dwc->chan),
  379. "BUG: All descriptors done, but channel not idle!\n");
  380. /* Try to continue after resetting the channel... */
  381. dwc_chan_disable(dw, dwc);
  382. dwc_dostart_first_queued(dwc);
  383. spin_unlock_irqrestore(&dwc->lock, flags);
  384. }
  385. static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
  386. {
  387. dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  388. lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
  389. }
  390. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  391. {
  392. struct dw_desc *bad_desc;
  393. struct dw_desc *child;
  394. unsigned long flags;
  395. dwc_scan_descriptors(dw, dwc);
  396. spin_lock_irqsave(&dwc->lock, flags);
  397. /*
  398. * The descriptor currently at the head of the active list is
  399. * borked. Since we don't have any way to report errors, we'll
  400. * just have to scream loudly and try to carry on.
  401. */
  402. bad_desc = dwc_first_active(dwc);
  403. list_del_init(&bad_desc->desc_node);
  404. list_move(dwc->queue.next, dwc->active_list.prev);
  405. /* Clear the error flag and try to restart the controller */
  406. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  407. if (!list_empty(&dwc->active_list))
  408. dwc_dostart(dwc, dwc_first_active(dwc));
  409. /*
  410. * WARN may seem harsh, but since this only happens
  411. * when someone submits a bad physical address in a
  412. * descriptor, we should consider ourselves lucky that the
  413. * controller flagged an error instead of scribbling over
  414. * random memory locations.
  415. */
  416. dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
  417. " cookie: %d\n", bad_desc->txd.cookie);
  418. dwc_dump_lli(dwc, &bad_desc->lli);
  419. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  420. dwc_dump_lli(dwc, &child->lli);
  421. spin_unlock_irqrestore(&dwc->lock, flags);
  422. /* Pretend the descriptor completed successfully */
  423. dwc_descriptor_complete(dwc, bad_desc, true);
  424. }
  425. /* --------------------- Cyclic DMA API extensions -------------------- */
  426. dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
  427. {
  428. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  429. return channel_readl(dwc, SAR);
  430. }
  431. EXPORT_SYMBOL(dw_dma_get_src_addr);
  432. dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
  433. {
  434. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  435. return channel_readl(dwc, DAR);
  436. }
  437. EXPORT_SYMBOL(dw_dma_get_dst_addr);
  438. /* Called with dwc->lock held and all DMAC interrupts disabled */
  439. static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
  440. u32 status_block, u32 status_err, u32 status_xfer)
  441. {
  442. unsigned long flags;
  443. if (status_block & dwc->mask) {
  444. void (*callback)(void *param);
  445. void *callback_param;
  446. dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
  447. channel_readl(dwc, LLP));
  448. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  449. callback = dwc->cdesc->period_callback;
  450. callback_param = dwc->cdesc->period_callback_param;
  451. if (callback)
  452. callback(callback_param);
  453. }
  454. /*
  455. * Error and transfer complete are highly unlikely, and will most
  456. * likely be due to a configuration error by the user.
  457. */
  458. if (unlikely(status_err & dwc->mask) ||
  459. unlikely(status_xfer & dwc->mask)) {
  460. int i;
  461. dev_err(chan2dev(&dwc->chan),
  462. "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
  463. status_xfer ? "xfer" : "error");
  464. spin_lock_irqsave(&dwc->lock, flags);
  465. dwc_dump_chan_regs(dwc);
  466. dwc_chan_disable(dw, dwc);
  467. /* Make sure DMA does not restart by loading a new list */
  468. channel_writel(dwc, LLP, 0);
  469. channel_writel(dwc, CTL_LO, 0);
  470. channel_writel(dwc, CTL_HI, 0);
  471. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  472. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  473. dma_writel(dw, CLEAR.XFER, dwc->mask);
  474. for (i = 0; i < dwc->cdesc->periods; i++)
  475. dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
  476. spin_unlock_irqrestore(&dwc->lock, flags);
  477. }
  478. /* Re-enable interrupts */
  479. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  480. }
  481. /* ------------------------------------------------------------------------- */
  482. static void dw_dma_tasklet(unsigned long data)
  483. {
  484. struct dw_dma *dw = (struct dw_dma *)data;
  485. struct dw_dma_chan *dwc;
  486. u32 status_block;
  487. u32 status_xfer;
  488. u32 status_err;
  489. int i;
  490. status_block = dma_readl(dw, RAW.BLOCK);
  491. status_xfer = dma_readl(dw, RAW.XFER);
  492. status_err = dma_readl(dw, RAW.ERROR);
  493. dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
  494. for (i = 0; i < dw->dma.chancnt; i++) {
  495. dwc = &dw->chan[i];
  496. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  497. dwc_handle_cyclic(dw, dwc, status_block, status_err,
  498. status_xfer);
  499. else if (status_err & (1 << i))
  500. dwc_handle_error(dw, dwc);
  501. else if (status_xfer & (1 << i))
  502. dwc_scan_descriptors(dw, dwc);
  503. }
  504. /* Re-enable interrupts */
  505. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  506. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  507. }
  508. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  509. {
  510. struct dw_dma *dw = dev_id;
  511. u32 status;
  512. /* Check if we have any interrupt from the DMAC which is not in use */
  513. if (!dw->in_use)
  514. return IRQ_NONE;
  515. status = dma_readl(dw, STATUS_INT);
  516. dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
  517. /* Check if we have any interrupt from the DMAC */
  518. if (!status)
  519. return IRQ_NONE;
  520. /*
  521. * Just disable the interrupts. We'll turn them back on in the
  522. * softirq handler.
  523. */
  524. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  525. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  526. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  527. status = dma_readl(dw, STATUS_INT);
  528. if (status) {
  529. dev_err(dw->dma.dev,
  530. "BUG: Unexpected interrupts pending: 0x%x\n",
  531. status);
  532. /* Try to recover */
  533. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  534. channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
  535. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  536. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  537. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  538. }
  539. tasklet_schedule(&dw->tasklet);
  540. return IRQ_HANDLED;
  541. }
  542. /*----------------------------------------------------------------------*/
  543. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  544. {
  545. struct dw_desc *desc = txd_to_dw_desc(tx);
  546. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  547. dma_cookie_t cookie;
  548. unsigned long flags;
  549. spin_lock_irqsave(&dwc->lock, flags);
  550. cookie = dma_cookie_assign(tx);
  551. /*
  552. * REVISIT: We should attempt to chain as many descriptors as
  553. * possible, perhaps even appending to those already submitted
  554. * for DMA. But this is hard to do in a race-free manner.
  555. */
  556. dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__, desc->txd.cookie);
  557. list_add_tail(&desc->desc_node, &dwc->queue);
  558. spin_unlock_irqrestore(&dwc->lock, flags);
  559. return cookie;
  560. }
  561. static struct dma_async_tx_descriptor *
  562. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  563. size_t len, unsigned long flags)
  564. {
  565. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  566. struct dw_dma *dw = to_dw_dma(chan->device);
  567. struct dw_desc *desc;
  568. struct dw_desc *first;
  569. struct dw_desc *prev;
  570. size_t xfer_count;
  571. size_t offset;
  572. unsigned int src_width;
  573. unsigned int dst_width;
  574. unsigned int data_width;
  575. u32 ctllo;
  576. dev_vdbg(chan2dev(chan),
  577. "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
  578. &dest, &src, len, flags);
  579. if (unlikely(!len)) {
  580. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  581. return NULL;
  582. }
  583. dwc->direction = DMA_MEM_TO_MEM;
  584. data_width = min_t(unsigned int, dw->data_width[dwc->src_master],
  585. dw->data_width[dwc->dst_master]);
  586. src_width = dst_width = min_t(unsigned int, data_width,
  587. dwc_fast_ffs(src | dest | len));
  588. ctllo = DWC_DEFAULT_CTLLO(chan)
  589. | DWC_CTLL_DST_WIDTH(dst_width)
  590. | DWC_CTLL_SRC_WIDTH(src_width)
  591. | DWC_CTLL_DST_INC
  592. | DWC_CTLL_SRC_INC
  593. | DWC_CTLL_FC_M2M;
  594. prev = first = NULL;
  595. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  596. xfer_count = min_t(size_t, (len - offset) >> src_width,
  597. dwc->block_size);
  598. desc = dwc_desc_get(dwc);
  599. if (!desc)
  600. goto err_desc_get;
  601. desc->lli.sar = src + offset;
  602. desc->lli.dar = dest + offset;
  603. desc->lli.ctllo = ctllo;
  604. desc->lli.ctlhi = xfer_count;
  605. desc->len = xfer_count << src_width;
  606. if (!first) {
  607. first = desc;
  608. } else {
  609. prev->lli.llp = desc->txd.phys;
  610. list_add_tail(&desc->desc_node,
  611. &first->tx_list);
  612. }
  613. prev = desc;
  614. }
  615. if (flags & DMA_PREP_INTERRUPT)
  616. /* Trigger interrupt after last block */
  617. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  618. prev->lli.llp = 0;
  619. first->txd.flags = flags;
  620. first->total_len = len;
  621. return &first->txd;
  622. err_desc_get:
  623. dwc_desc_put(dwc, first);
  624. return NULL;
  625. }
  626. static struct dma_async_tx_descriptor *
  627. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  628. unsigned int sg_len, enum dma_transfer_direction direction,
  629. unsigned long flags, void *context)
  630. {
  631. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  632. struct dw_dma *dw = to_dw_dma(chan->device);
  633. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  634. struct dw_desc *prev;
  635. struct dw_desc *first;
  636. u32 ctllo;
  637. dma_addr_t reg;
  638. unsigned int reg_width;
  639. unsigned int mem_width;
  640. unsigned int data_width;
  641. unsigned int i;
  642. struct scatterlist *sg;
  643. size_t total_len = 0;
  644. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  645. if (unlikely(!is_slave_direction(direction) || !sg_len))
  646. return NULL;
  647. dwc->direction = direction;
  648. prev = first = NULL;
  649. switch (direction) {
  650. case DMA_MEM_TO_DEV:
  651. reg_width = __ffs(sconfig->dst_addr_width);
  652. reg = sconfig->dst_addr;
  653. ctllo = (DWC_DEFAULT_CTLLO(chan)
  654. | DWC_CTLL_DST_WIDTH(reg_width)
  655. | DWC_CTLL_DST_FIX
  656. | DWC_CTLL_SRC_INC);
  657. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  658. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  659. data_width = dw->data_width[dwc->src_master];
  660. for_each_sg(sgl, sg, sg_len, i) {
  661. struct dw_desc *desc;
  662. u32 len, dlen, mem;
  663. mem = sg_dma_address(sg);
  664. len = sg_dma_len(sg);
  665. mem_width = min_t(unsigned int,
  666. data_width, dwc_fast_ffs(mem | len));
  667. slave_sg_todev_fill_desc:
  668. desc = dwc_desc_get(dwc);
  669. if (!desc)
  670. goto err_desc_get;
  671. desc->lli.sar = mem;
  672. desc->lli.dar = reg;
  673. desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
  674. if ((len >> mem_width) > dwc->block_size) {
  675. dlen = dwc->block_size << mem_width;
  676. mem += dlen;
  677. len -= dlen;
  678. } else {
  679. dlen = len;
  680. len = 0;
  681. }
  682. desc->lli.ctlhi = dlen >> mem_width;
  683. desc->len = dlen;
  684. if (!first) {
  685. first = desc;
  686. } else {
  687. prev->lli.llp = desc->txd.phys;
  688. list_add_tail(&desc->desc_node,
  689. &first->tx_list);
  690. }
  691. prev = desc;
  692. total_len += dlen;
  693. if (len)
  694. goto slave_sg_todev_fill_desc;
  695. }
  696. break;
  697. case DMA_DEV_TO_MEM:
  698. reg_width = __ffs(sconfig->src_addr_width);
  699. reg = sconfig->src_addr;
  700. ctllo = (DWC_DEFAULT_CTLLO(chan)
  701. | DWC_CTLL_SRC_WIDTH(reg_width)
  702. | DWC_CTLL_DST_INC
  703. | DWC_CTLL_SRC_FIX);
  704. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  705. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  706. data_width = dw->data_width[dwc->dst_master];
  707. for_each_sg(sgl, sg, sg_len, i) {
  708. struct dw_desc *desc;
  709. u32 len, dlen, mem;
  710. mem = sg_dma_address(sg);
  711. len = sg_dma_len(sg);
  712. mem_width = min_t(unsigned int,
  713. data_width, dwc_fast_ffs(mem | len));
  714. slave_sg_fromdev_fill_desc:
  715. desc = dwc_desc_get(dwc);
  716. if (!desc)
  717. goto err_desc_get;
  718. desc->lli.sar = reg;
  719. desc->lli.dar = mem;
  720. desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
  721. if ((len >> reg_width) > dwc->block_size) {
  722. dlen = dwc->block_size << reg_width;
  723. mem += dlen;
  724. len -= dlen;
  725. } else {
  726. dlen = len;
  727. len = 0;
  728. }
  729. desc->lli.ctlhi = dlen >> reg_width;
  730. desc->len = dlen;
  731. if (!first) {
  732. first = desc;
  733. } else {
  734. prev->lli.llp = desc->txd.phys;
  735. list_add_tail(&desc->desc_node,
  736. &first->tx_list);
  737. }
  738. prev = desc;
  739. total_len += dlen;
  740. if (len)
  741. goto slave_sg_fromdev_fill_desc;
  742. }
  743. break;
  744. default:
  745. return NULL;
  746. }
  747. if (flags & DMA_PREP_INTERRUPT)
  748. /* Trigger interrupt after last block */
  749. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  750. prev->lli.llp = 0;
  751. first->total_len = total_len;
  752. return &first->txd;
  753. err_desc_get:
  754. dev_err(chan2dev(chan),
  755. "not enough descriptors available. Direction %d\n", direction);
  756. dwc_desc_put(dwc, first);
  757. return NULL;
  758. }
  759. bool dw_dma_filter(struct dma_chan *chan, void *param)
  760. {
  761. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  762. struct dw_dma_slave *dws = param;
  763. if (!dws || dws->dma_dev != chan->device->dev)
  764. return false;
  765. /* We have to copy data since dws can be temporary storage */
  766. dwc->src_id = dws->src_id;
  767. dwc->dst_id = dws->dst_id;
  768. dwc->src_master = dws->src_master;
  769. dwc->dst_master = dws->dst_master;
  770. return true;
  771. }
  772. EXPORT_SYMBOL_GPL(dw_dma_filter);
  773. /*
  774. * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
  775. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
  776. *
  777. * NOTE: burst size 2 is not supported by controller.
  778. *
  779. * This can be done by finding least significant bit set: n & (n - 1)
  780. */
  781. static inline void convert_burst(u32 *maxburst)
  782. {
  783. if (*maxburst > 1)
  784. *maxburst = fls(*maxburst) - 2;
  785. else
  786. *maxburst = 0;
  787. }
  788. static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
  789. {
  790. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  791. /* Check if chan will be configured for slave transfers */
  792. if (!is_slave_direction(sconfig->direction))
  793. return -EINVAL;
  794. memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
  795. dwc->direction = sconfig->direction;
  796. convert_burst(&dwc->dma_sconfig.src_maxburst);
  797. convert_burst(&dwc->dma_sconfig.dst_maxburst);
  798. return 0;
  799. }
  800. static int dwc_pause(struct dma_chan *chan)
  801. {
  802. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  803. unsigned long flags;
  804. unsigned int count = 20; /* timeout iterations */
  805. u32 cfglo;
  806. spin_lock_irqsave(&dwc->lock, flags);
  807. cfglo = channel_readl(dwc, CFG_LO);
  808. channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
  809. while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
  810. udelay(2);
  811. dwc->paused = true;
  812. spin_unlock_irqrestore(&dwc->lock, flags);
  813. return 0;
  814. }
  815. static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
  816. {
  817. u32 cfglo = channel_readl(dwc, CFG_LO);
  818. channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
  819. dwc->paused = false;
  820. }
  821. static int dwc_resume(struct dma_chan *chan)
  822. {
  823. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  824. unsigned long flags;
  825. if (!dwc->paused)
  826. return 0;
  827. spin_lock_irqsave(&dwc->lock, flags);
  828. dwc_chan_resume(dwc);
  829. spin_unlock_irqrestore(&dwc->lock, flags);
  830. return 0;
  831. }
  832. static int dwc_terminate_all(struct dma_chan *chan)
  833. {
  834. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  835. struct dw_dma *dw = to_dw_dma(chan->device);
  836. struct dw_desc *desc, *_desc;
  837. unsigned long flags;
  838. LIST_HEAD(list);
  839. spin_lock_irqsave(&dwc->lock, flags);
  840. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  841. dwc_chan_disable(dw, dwc);
  842. dwc_chan_resume(dwc);
  843. /* active_list entries will end up before queued entries */
  844. list_splice_init(&dwc->queue, &list);
  845. list_splice_init(&dwc->active_list, &list);
  846. spin_unlock_irqrestore(&dwc->lock, flags);
  847. /* Flush all pending and queued descriptors */
  848. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  849. dwc_descriptor_complete(dwc, desc, false);
  850. return 0;
  851. }
  852. static inline u32 dwc_get_residue(struct dw_dma_chan *dwc)
  853. {
  854. unsigned long flags;
  855. u32 residue;
  856. spin_lock_irqsave(&dwc->lock, flags);
  857. residue = dwc->residue;
  858. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
  859. residue -= dwc_get_sent(dwc);
  860. spin_unlock_irqrestore(&dwc->lock, flags);
  861. return residue;
  862. }
  863. static enum dma_status
  864. dwc_tx_status(struct dma_chan *chan,
  865. dma_cookie_t cookie,
  866. struct dma_tx_state *txstate)
  867. {
  868. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  869. enum dma_status ret;
  870. ret = dma_cookie_status(chan, cookie, txstate);
  871. if (ret == DMA_COMPLETE)
  872. return ret;
  873. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  874. ret = dma_cookie_status(chan, cookie, txstate);
  875. if (ret != DMA_COMPLETE)
  876. dma_set_residue(txstate, dwc_get_residue(dwc));
  877. if (dwc->paused && ret == DMA_IN_PROGRESS)
  878. return DMA_PAUSED;
  879. return ret;
  880. }
  881. static void dwc_issue_pending(struct dma_chan *chan)
  882. {
  883. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  884. unsigned long flags;
  885. spin_lock_irqsave(&dwc->lock, flags);
  886. if (list_empty(&dwc->active_list))
  887. dwc_dostart_first_queued(dwc);
  888. spin_unlock_irqrestore(&dwc->lock, flags);
  889. }
  890. /*----------------------------------------------------------------------*/
  891. static void dw_dma_off(struct dw_dma *dw)
  892. {
  893. int i;
  894. dma_writel(dw, CFG, 0);
  895. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  896. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  897. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  898. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  899. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  900. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  901. cpu_relax();
  902. for (i = 0; i < dw->dma.chancnt; i++)
  903. dw->chan[i].initialized = false;
  904. }
  905. static void dw_dma_on(struct dw_dma *dw)
  906. {
  907. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  908. }
  909. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  910. {
  911. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  912. struct dw_dma *dw = to_dw_dma(chan->device);
  913. struct dw_desc *desc;
  914. int i;
  915. unsigned long flags;
  916. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  917. /* ASSERT: channel is idle */
  918. if (dma_readl(dw, CH_EN) & dwc->mask) {
  919. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  920. return -EIO;
  921. }
  922. dma_cookie_init(chan);
  923. /*
  924. * NOTE: some controllers may have additional features that we
  925. * need to initialize here, like "scatter-gather" (which
  926. * doesn't mean what you think it means), and status writeback.
  927. */
  928. /* Enable controller here if needed */
  929. if (!dw->in_use)
  930. dw_dma_on(dw);
  931. dw->in_use |= dwc->mask;
  932. spin_lock_irqsave(&dwc->lock, flags);
  933. i = dwc->descs_allocated;
  934. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  935. dma_addr_t phys;
  936. spin_unlock_irqrestore(&dwc->lock, flags);
  937. desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
  938. if (!desc)
  939. goto err_desc_alloc;
  940. memset(desc, 0, sizeof(struct dw_desc));
  941. INIT_LIST_HEAD(&desc->tx_list);
  942. dma_async_tx_descriptor_init(&desc->txd, chan);
  943. desc->txd.tx_submit = dwc_tx_submit;
  944. desc->txd.flags = DMA_CTRL_ACK;
  945. desc->txd.phys = phys;
  946. dwc_desc_put(dwc, desc);
  947. spin_lock_irqsave(&dwc->lock, flags);
  948. i = ++dwc->descs_allocated;
  949. }
  950. spin_unlock_irqrestore(&dwc->lock, flags);
  951. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  952. return i;
  953. err_desc_alloc:
  954. dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
  955. return i;
  956. }
  957. static void dwc_free_chan_resources(struct dma_chan *chan)
  958. {
  959. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  960. struct dw_dma *dw = to_dw_dma(chan->device);
  961. struct dw_desc *desc, *_desc;
  962. unsigned long flags;
  963. LIST_HEAD(list);
  964. dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
  965. dwc->descs_allocated);
  966. /* ASSERT: channel is idle */
  967. BUG_ON(!list_empty(&dwc->active_list));
  968. BUG_ON(!list_empty(&dwc->queue));
  969. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  970. spin_lock_irqsave(&dwc->lock, flags);
  971. list_splice_init(&dwc->free_list, &list);
  972. dwc->descs_allocated = 0;
  973. dwc->initialized = false;
  974. /* Disable interrupts */
  975. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  976. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  977. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  978. spin_unlock_irqrestore(&dwc->lock, flags);
  979. /* Disable controller in case it was a last user */
  980. dw->in_use &= ~dwc->mask;
  981. if (!dw->in_use)
  982. dw_dma_off(dw);
  983. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  984. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  985. dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
  986. }
  987. dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
  988. }
  989. /* --------------------- Cyclic DMA API extensions -------------------- */
  990. /**
  991. * dw_dma_cyclic_start - start the cyclic DMA transfer
  992. * @chan: the DMA channel to start
  993. *
  994. * Must be called with soft interrupts disabled. Returns zero on success or
  995. * -errno on failure.
  996. */
  997. int dw_dma_cyclic_start(struct dma_chan *chan)
  998. {
  999. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1000. struct dw_dma *dw = to_dw_dma(chan->device);
  1001. unsigned long flags;
  1002. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  1003. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  1004. return -ENODEV;
  1005. }
  1006. spin_lock_irqsave(&dwc->lock, flags);
  1007. /* Enable interrupts to perform cyclic transfer */
  1008. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  1009. dwc_dostart(dwc, dwc->cdesc->desc[0]);
  1010. spin_unlock_irqrestore(&dwc->lock, flags);
  1011. return 0;
  1012. }
  1013. EXPORT_SYMBOL(dw_dma_cyclic_start);
  1014. /**
  1015. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  1016. * @chan: the DMA channel to stop
  1017. *
  1018. * Must be called with soft interrupts disabled.
  1019. */
  1020. void dw_dma_cyclic_stop(struct dma_chan *chan)
  1021. {
  1022. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1023. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1024. unsigned long flags;
  1025. spin_lock_irqsave(&dwc->lock, flags);
  1026. dwc_chan_disable(dw, dwc);
  1027. spin_unlock_irqrestore(&dwc->lock, flags);
  1028. }
  1029. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  1030. /**
  1031. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  1032. * @chan: the DMA channel to prepare
  1033. * @buf_addr: physical DMA address where the buffer starts
  1034. * @buf_len: total number of bytes for the entire buffer
  1035. * @period_len: number of bytes for each period
  1036. * @direction: transfer direction, to or from device
  1037. *
  1038. * Must be called before trying to start the transfer. Returns a valid struct
  1039. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  1040. */
  1041. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  1042. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  1043. enum dma_transfer_direction direction)
  1044. {
  1045. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1046. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  1047. struct dw_cyclic_desc *cdesc;
  1048. struct dw_cyclic_desc *retval = NULL;
  1049. struct dw_desc *desc;
  1050. struct dw_desc *last = NULL;
  1051. unsigned long was_cyclic;
  1052. unsigned int reg_width;
  1053. unsigned int periods;
  1054. unsigned int i;
  1055. unsigned long flags;
  1056. spin_lock_irqsave(&dwc->lock, flags);
  1057. if (dwc->nollp) {
  1058. spin_unlock_irqrestore(&dwc->lock, flags);
  1059. dev_dbg(chan2dev(&dwc->chan),
  1060. "channel doesn't support LLP transfers\n");
  1061. return ERR_PTR(-EINVAL);
  1062. }
  1063. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  1064. spin_unlock_irqrestore(&dwc->lock, flags);
  1065. dev_dbg(chan2dev(&dwc->chan),
  1066. "queue and/or active list are not empty\n");
  1067. return ERR_PTR(-EBUSY);
  1068. }
  1069. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1070. spin_unlock_irqrestore(&dwc->lock, flags);
  1071. if (was_cyclic) {
  1072. dev_dbg(chan2dev(&dwc->chan),
  1073. "channel already prepared for cyclic DMA\n");
  1074. return ERR_PTR(-EBUSY);
  1075. }
  1076. retval = ERR_PTR(-EINVAL);
  1077. if (unlikely(!is_slave_direction(direction)))
  1078. goto out_err;
  1079. dwc->direction = direction;
  1080. if (direction == DMA_MEM_TO_DEV)
  1081. reg_width = __ffs(sconfig->dst_addr_width);
  1082. else
  1083. reg_width = __ffs(sconfig->src_addr_width);
  1084. periods = buf_len / period_len;
  1085. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  1086. if (period_len > (dwc->block_size << reg_width))
  1087. goto out_err;
  1088. if (unlikely(period_len & ((1 << reg_width) - 1)))
  1089. goto out_err;
  1090. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1091. goto out_err;
  1092. retval = ERR_PTR(-ENOMEM);
  1093. if (periods > NR_DESCS_PER_CHANNEL)
  1094. goto out_err;
  1095. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  1096. if (!cdesc)
  1097. goto out_err;
  1098. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  1099. if (!cdesc->desc)
  1100. goto out_err_alloc;
  1101. for (i = 0; i < periods; i++) {
  1102. desc = dwc_desc_get(dwc);
  1103. if (!desc)
  1104. goto out_err_desc_get;
  1105. switch (direction) {
  1106. case DMA_MEM_TO_DEV:
  1107. desc->lli.dar = sconfig->dst_addr;
  1108. desc->lli.sar = buf_addr + (period_len * i);
  1109. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
  1110. | DWC_CTLL_DST_WIDTH(reg_width)
  1111. | DWC_CTLL_SRC_WIDTH(reg_width)
  1112. | DWC_CTLL_DST_FIX
  1113. | DWC_CTLL_SRC_INC
  1114. | DWC_CTLL_INT_EN);
  1115. desc->lli.ctllo |= sconfig->device_fc ?
  1116. DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  1117. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  1118. break;
  1119. case DMA_DEV_TO_MEM:
  1120. desc->lli.dar = buf_addr + (period_len * i);
  1121. desc->lli.sar = sconfig->src_addr;
  1122. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
  1123. | DWC_CTLL_SRC_WIDTH(reg_width)
  1124. | DWC_CTLL_DST_WIDTH(reg_width)
  1125. | DWC_CTLL_DST_INC
  1126. | DWC_CTLL_SRC_FIX
  1127. | DWC_CTLL_INT_EN);
  1128. desc->lli.ctllo |= sconfig->device_fc ?
  1129. DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  1130. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  1131. break;
  1132. default:
  1133. break;
  1134. }
  1135. desc->lli.ctlhi = (period_len >> reg_width);
  1136. cdesc->desc[i] = desc;
  1137. if (last)
  1138. last->lli.llp = desc->txd.phys;
  1139. last = desc;
  1140. }
  1141. /* Let's make a cyclic list */
  1142. last->lli.llp = cdesc->desc[0]->txd.phys;
  1143. dev_dbg(chan2dev(&dwc->chan),
  1144. "cyclic prepared buf %pad len %zu period %zu periods %d\n",
  1145. &buf_addr, buf_len, period_len, periods);
  1146. cdesc->periods = periods;
  1147. dwc->cdesc = cdesc;
  1148. return cdesc;
  1149. out_err_desc_get:
  1150. while (i--)
  1151. dwc_desc_put(dwc, cdesc->desc[i]);
  1152. out_err_alloc:
  1153. kfree(cdesc);
  1154. out_err:
  1155. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1156. return (struct dw_cyclic_desc *)retval;
  1157. }
  1158. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  1159. /**
  1160. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  1161. * @chan: the DMA channel to free
  1162. */
  1163. void dw_dma_cyclic_free(struct dma_chan *chan)
  1164. {
  1165. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1166. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1167. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1168. int i;
  1169. unsigned long flags;
  1170. dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
  1171. if (!cdesc)
  1172. return;
  1173. spin_lock_irqsave(&dwc->lock, flags);
  1174. dwc_chan_disable(dw, dwc);
  1175. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  1176. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1177. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1178. spin_unlock_irqrestore(&dwc->lock, flags);
  1179. for (i = 0; i < cdesc->periods; i++)
  1180. dwc_desc_put(dwc, cdesc->desc[i]);
  1181. kfree(cdesc->desc);
  1182. kfree(cdesc);
  1183. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1184. }
  1185. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1186. /*----------------------------------------------------------------------*/
  1187. int dw_dma_probe(struct dw_dma_chip *chip, struct dw_dma_platform_data *pdata)
  1188. {
  1189. struct dw_dma *dw;
  1190. bool autocfg = false;
  1191. unsigned int dw_params;
  1192. unsigned int max_blk_size = 0;
  1193. int err;
  1194. int i;
  1195. dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
  1196. if (!dw)
  1197. return -ENOMEM;
  1198. dw->regs = chip->regs;
  1199. chip->dw = dw;
  1200. pm_runtime_get_sync(chip->dev);
  1201. if (!pdata) {
  1202. dw_params = dma_read_byaddr(chip->regs, DW_PARAMS);
  1203. dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
  1204. autocfg = dw_params >> DW_PARAMS_EN & 1;
  1205. if (!autocfg) {
  1206. err = -EINVAL;
  1207. goto err_pdata;
  1208. }
  1209. pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL);
  1210. if (!pdata) {
  1211. err = -ENOMEM;
  1212. goto err_pdata;
  1213. }
  1214. /* Get hardware configuration parameters */
  1215. pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1;
  1216. pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
  1217. for (i = 0; i < pdata->nr_masters; i++) {
  1218. pdata->data_width[i] =
  1219. (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
  1220. }
  1221. max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
  1222. /* Fill platform data with the default values */
  1223. pdata->is_private = true;
  1224. pdata->is_memcpy = true;
  1225. pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
  1226. pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
  1227. } else if (pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
  1228. err = -EINVAL;
  1229. goto err_pdata;
  1230. }
  1231. dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan),
  1232. GFP_KERNEL);
  1233. if (!dw->chan) {
  1234. err = -ENOMEM;
  1235. goto err_pdata;
  1236. }
  1237. /* Get hardware configuration parameters */
  1238. dw->nr_masters = pdata->nr_masters;
  1239. for (i = 0; i < dw->nr_masters; i++)
  1240. dw->data_width[i] = pdata->data_width[i];
  1241. /* Calculate all channel mask before DMA setup */
  1242. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  1243. /* Force dma off, just in case */
  1244. dw_dma_off(dw);
  1245. /* Create a pool of consistent memory blocks for hardware descriptors */
  1246. dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", chip->dev,
  1247. sizeof(struct dw_desc), 4, 0);
  1248. if (!dw->desc_pool) {
  1249. dev_err(chip->dev, "No memory for descriptors dma pool\n");
  1250. err = -ENOMEM;
  1251. goto err_pdata;
  1252. }
  1253. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1254. err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
  1255. "dw_dmac", dw);
  1256. if (err)
  1257. goto err_pdata;
  1258. INIT_LIST_HEAD(&dw->dma.channels);
  1259. for (i = 0; i < pdata->nr_channels; i++) {
  1260. struct dw_dma_chan *dwc = &dw->chan[i];
  1261. dwc->chan.device = &dw->dma;
  1262. dma_cookie_init(&dwc->chan);
  1263. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  1264. list_add_tail(&dwc->chan.device_node,
  1265. &dw->dma.channels);
  1266. else
  1267. list_add(&dwc->chan.device_node, &dw->dma.channels);
  1268. /* 7 is highest priority & 0 is lowest. */
  1269. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  1270. dwc->priority = pdata->nr_channels - i - 1;
  1271. else
  1272. dwc->priority = i;
  1273. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1274. spin_lock_init(&dwc->lock);
  1275. dwc->mask = 1 << i;
  1276. INIT_LIST_HEAD(&dwc->active_list);
  1277. INIT_LIST_HEAD(&dwc->queue);
  1278. INIT_LIST_HEAD(&dwc->free_list);
  1279. channel_clear_bit(dw, CH_EN, dwc->mask);
  1280. dwc->direction = DMA_TRANS_NONE;
  1281. /* Hardware configuration */
  1282. if (autocfg) {
  1283. unsigned int dwc_params;
  1284. unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1;
  1285. void __iomem *addr = chip->regs + r * sizeof(u32);
  1286. dwc_params = dma_read_byaddr(addr, DWC_PARAMS);
  1287. dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
  1288. dwc_params);
  1289. /*
  1290. * Decode maximum block size for given channel. The
  1291. * stored 4 bit value represents blocks from 0x00 for 3
  1292. * up to 0x0a for 4095.
  1293. */
  1294. dwc->block_size =
  1295. (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
  1296. dwc->nollp =
  1297. (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
  1298. } else {
  1299. dwc->block_size = pdata->block_size;
  1300. /* Check if channel supports multi block transfer */
  1301. channel_writel(dwc, LLP, 0xfffffffc);
  1302. dwc->nollp =
  1303. (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
  1304. channel_writel(dwc, LLP, 0);
  1305. }
  1306. }
  1307. /* Clear all interrupts on all channels. */
  1308. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1309. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1310. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1311. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1312. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1313. /* Set capabilities */
  1314. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1315. if (pdata->is_private)
  1316. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1317. if (pdata->is_memcpy)
  1318. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1319. dw->dma.dev = chip->dev;
  1320. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1321. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1322. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1323. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1324. dw->dma.device_config = dwc_config;
  1325. dw->dma.device_pause = dwc_pause;
  1326. dw->dma.device_resume = dwc_resume;
  1327. dw->dma.device_terminate_all = dwc_terminate_all;
  1328. dw->dma.device_tx_status = dwc_tx_status;
  1329. dw->dma.device_issue_pending = dwc_issue_pending;
  1330. /* DMA capabilities */
  1331. dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS;
  1332. dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS;
  1333. dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  1334. BIT(DMA_MEM_TO_MEM);
  1335. dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1336. err = dma_async_device_register(&dw->dma);
  1337. if (err)
  1338. goto err_dma_register;
  1339. dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
  1340. pdata->nr_channels);
  1341. pm_runtime_put_sync_suspend(chip->dev);
  1342. return 0;
  1343. err_dma_register:
  1344. free_irq(chip->irq, dw);
  1345. err_pdata:
  1346. pm_runtime_put_sync_suspend(chip->dev);
  1347. return err;
  1348. }
  1349. EXPORT_SYMBOL_GPL(dw_dma_probe);
  1350. int dw_dma_remove(struct dw_dma_chip *chip)
  1351. {
  1352. struct dw_dma *dw = chip->dw;
  1353. struct dw_dma_chan *dwc, *_dwc;
  1354. pm_runtime_get_sync(chip->dev);
  1355. dw_dma_off(dw);
  1356. dma_async_device_unregister(&dw->dma);
  1357. free_irq(chip->irq, dw);
  1358. tasklet_kill(&dw->tasklet);
  1359. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1360. chan.device_node) {
  1361. list_del(&dwc->chan.device_node);
  1362. channel_clear_bit(dw, CH_EN, dwc->mask);
  1363. }
  1364. pm_runtime_put_sync_suspend(chip->dev);
  1365. return 0;
  1366. }
  1367. EXPORT_SYMBOL_GPL(dw_dma_remove);
  1368. int dw_dma_disable(struct dw_dma_chip *chip)
  1369. {
  1370. struct dw_dma *dw = chip->dw;
  1371. dw_dma_off(dw);
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL_GPL(dw_dma_disable);
  1375. int dw_dma_enable(struct dw_dma_chip *chip)
  1376. {
  1377. struct dw_dma *dw = chip->dw;
  1378. dw_dma_on(dw);
  1379. return 0;
  1380. }
  1381. EXPORT_SYMBOL_GPL(dw_dma_enable);
  1382. MODULE_LICENSE("GPL v2");
  1383. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
  1384. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1385. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");