core.c 43 KB

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