core.c 43 KB

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