core.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Core driver for the Synopsys DesignWare DMA Controller
  4. *
  5. * Copyright (C) 2007-2008 Atmel Corporation
  6. * Copyright (C) 2010-2011 ST Microelectronics
  7. * Copyright (C) 2013 Intel Corporation
  8. */
  9. #include <linux/bitops.h>
  10. #include <linux/delay.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/log2.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/slab.h>
  22. #include <linux/pm_runtime.h>
  23. #include "../dmaengine.h"
  24. #include "internal.h"
  25. /*
  26. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  27. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  28. * of which use ARM any more). See the "Databook" from Synopsys for
  29. * information beyond what licensees probably provide.
  30. */
  31. /* The set of bus widths supported by the DMA controller */
  32. #define DW_DMA_BUSWIDTHS \
  33. BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
  34. BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
  35. BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
  36. BIT(DMA_SLAVE_BUSWIDTH_4_BYTES)
  37. /*----------------------------------------------------------------------*/
  38. static struct device *chan2dev(struct dma_chan *chan)
  39. {
  40. return &chan->dev->device;
  41. }
  42. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  43. {
  44. return to_dw_desc(dwc->active_list.next);
  45. }
  46. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  47. {
  48. struct dw_desc *desc = txd_to_dw_desc(tx);
  49. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  50. dma_cookie_t cookie;
  51. unsigned long flags;
  52. spin_lock_irqsave(&dwc->lock, flags);
  53. cookie = dma_cookie_assign(tx);
  54. /*
  55. * REVISIT: We should attempt to chain as many descriptors as
  56. * possible, perhaps even appending to those already submitted
  57. * for DMA. But this is hard to do in a race-free manner.
  58. */
  59. list_add_tail(&desc->desc_node, &dwc->queue);
  60. spin_unlock_irqrestore(&dwc->lock, flags);
  61. dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n",
  62. __func__, desc->txd.cookie);
  63. return cookie;
  64. }
  65. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  66. {
  67. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  68. struct dw_desc *desc;
  69. dma_addr_t phys;
  70. desc = dma_pool_zalloc(dw->desc_pool, GFP_ATOMIC, &phys);
  71. if (!desc)
  72. return NULL;
  73. dwc->descs_allocated++;
  74. INIT_LIST_HEAD(&desc->tx_list);
  75. dma_async_tx_descriptor_init(&desc->txd, &dwc->chan);
  76. desc->txd.tx_submit = dwc_tx_submit;
  77. desc->txd.flags = DMA_CTRL_ACK;
  78. desc->txd.phys = phys;
  79. return desc;
  80. }
  81. static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
  82. {
  83. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  84. struct dw_desc *child, *_next;
  85. if (unlikely(!desc))
  86. return;
  87. list_for_each_entry_safe(child, _next, &desc->tx_list, desc_node) {
  88. list_del(&child->desc_node);
  89. dma_pool_free(dw->desc_pool, child, child->txd.phys);
  90. dwc->descs_allocated--;
  91. }
  92. dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
  93. dwc->descs_allocated--;
  94. }
  95. static void dwc_initialize(struct dw_dma_chan *dwc)
  96. {
  97. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  98. dw->initialize_chan(dwc);
  99. /* Enable interrupts */
  100. channel_set_bit(dw, MASK.XFER, dwc->mask);
  101. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  102. }
  103. /*----------------------------------------------------------------------*/
  104. static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
  105. {
  106. dev_err(chan2dev(&dwc->chan),
  107. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  108. channel_readl(dwc, SAR),
  109. channel_readl(dwc, DAR),
  110. channel_readl(dwc, LLP),
  111. channel_readl(dwc, CTL_HI),
  112. channel_readl(dwc, CTL_LO));
  113. }
  114. static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
  115. {
  116. channel_clear_bit(dw, CH_EN, dwc->mask);
  117. while (dma_readl(dw, CH_EN) & dwc->mask)
  118. cpu_relax();
  119. }
  120. /*----------------------------------------------------------------------*/
  121. /* Perform single block transfer */
  122. static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
  123. struct dw_desc *desc)
  124. {
  125. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  126. u32 ctllo;
  127. /*
  128. * Software emulation of LLP mode relies on interrupts to continue
  129. * multi block transfer.
  130. */
  131. ctllo = lli_read(desc, ctllo) | DWC_CTLL_INT_EN;
  132. channel_writel(dwc, SAR, lli_read(desc, sar));
  133. channel_writel(dwc, DAR, lli_read(desc, dar));
  134. channel_writel(dwc, CTL_LO, ctllo);
  135. channel_writel(dwc, CTL_HI, lli_read(desc, ctlhi));
  136. channel_set_bit(dw, CH_EN, dwc->mask);
  137. /* Move pointer to next descriptor */
  138. dwc->tx_node_active = dwc->tx_node_active->next;
  139. }
  140. /* Called with dwc->lock held and bh disabled */
  141. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  142. {
  143. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  144. u8 lms = DWC_LLP_LMS(dwc->dws.m_master);
  145. unsigned long was_soft_llp;
  146. /* ASSERT: channel is idle */
  147. if (dma_readl(dw, CH_EN) & dwc->mask) {
  148. dev_err(chan2dev(&dwc->chan),
  149. "%s: BUG: Attempted to start non-idle channel\n",
  150. __func__);
  151. dwc_dump_chan_regs(dwc);
  152. /* The tasklet will hopefully advance the queue... */
  153. return;
  154. }
  155. if (dwc->nollp) {
  156. was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
  157. &dwc->flags);
  158. if (was_soft_llp) {
  159. dev_err(chan2dev(&dwc->chan),
  160. "BUG: Attempted to start new LLP transfer inside ongoing one\n");
  161. return;
  162. }
  163. dwc_initialize(dwc);
  164. first->residue = first->total_len;
  165. dwc->tx_node_active = &first->tx_list;
  166. /* Submit first block */
  167. dwc_do_single_block(dwc, first);
  168. return;
  169. }
  170. dwc_initialize(dwc);
  171. channel_writel(dwc, LLP, first->txd.phys | lms);
  172. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  173. channel_writel(dwc, CTL_HI, 0);
  174. channel_set_bit(dw, CH_EN, dwc->mask);
  175. }
  176. static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
  177. {
  178. struct dw_desc *desc;
  179. if (list_empty(&dwc->queue))
  180. return;
  181. list_move(dwc->queue.next, &dwc->active_list);
  182. desc = dwc_first_active(dwc);
  183. dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
  184. dwc_dostart(dwc, desc);
  185. }
  186. /*----------------------------------------------------------------------*/
  187. static void
  188. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
  189. bool callback_required)
  190. {
  191. struct dma_async_tx_descriptor *txd = &desc->txd;
  192. struct dw_desc *child;
  193. unsigned long flags;
  194. struct dmaengine_desc_callback cb;
  195. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  196. spin_lock_irqsave(&dwc->lock, flags);
  197. dma_cookie_complete(txd);
  198. if (callback_required)
  199. dmaengine_desc_get_callback(txd, &cb);
  200. else
  201. memset(&cb, 0, sizeof(cb));
  202. /* async_tx_ack */
  203. list_for_each_entry(child, &desc->tx_list, desc_node)
  204. async_tx_ack(&child->txd);
  205. async_tx_ack(&desc->txd);
  206. dwc_desc_put(dwc, desc);
  207. spin_unlock_irqrestore(&dwc->lock, flags);
  208. dmaengine_desc_callback_invoke(&cb, NULL);
  209. }
  210. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  211. {
  212. struct dw_desc *desc, *_desc;
  213. LIST_HEAD(list);
  214. unsigned long flags;
  215. spin_lock_irqsave(&dwc->lock, flags);
  216. if (dma_readl(dw, CH_EN) & dwc->mask) {
  217. dev_err(chan2dev(&dwc->chan),
  218. "BUG: XFER bit set, but channel not idle!\n");
  219. /* Try to continue after resetting the channel... */
  220. dwc_chan_disable(dw, dwc);
  221. }
  222. /*
  223. * Submit queued descriptors ASAP, i.e. before we go through
  224. * the completed ones.
  225. */
  226. list_splice_init(&dwc->active_list, &list);
  227. dwc_dostart_first_queued(dwc);
  228. spin_unlock_irqrestore(&dwc->lock, flags);
  229. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  230. dwc_descriptor_complete(dwc, desc, true);
  231. }
  232. /* Returns how many bytes were already received from source */
  233. static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
  234. {
  235. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  236. u32 ctlhi = channel_readl(dwc, CTL_HI);
  237. u32 ctllo = channel_readl(dwc, CTL_LO);
  238. return dw->block2bytes(dwc, ctlhi, ctllo >> 4 & 7);
  239. }
  240. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  241. {
  242. dma_addr_t llp;
  243. struct dw_desc *desc, *_desc;
  244. struct dw_desc *child;
  245. u32 status_xfer;
  246. unsigned long flags;
  247. spin_lock_irqsave(&dwc->lock, flags);
  248. llp = channel_readl(dwc, LLP);
  249. status_xfer = dma_readl(dw, RAW.XFER);
  250. if (status_xfer & dwc->mask) {
  251. /* Everything we've submitted is done */
  252. dma_writel(dw, CLEAR.XFER, dwc->mask);
  253. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  254. struct list_head *head, *active = dwc->tx_node_active;
  255. /*
  256. * We are inside first active descriptor.
  257. * Otherwise something is really wrong.
  258. */
  259. desc = dwc_first_active(dwc);
  260. head = &desc->tx_list;
  261. if (active != head) {
  262. /* Update residue to reflect last sent descriptor */
  263. if (active == head->next)
  264. desc->residue -= desc->len;
  265. else
  266. desc->residue -= to_dw_desc(active->prev)->len;
  267. child = to_dw_desc(active);
  268. /* Submit next block */
  269. dwc_do_single_block(dwc, child);
  270. spin_unlock_irqrestore(&dwc->lock, flags);
  271. return;
  272. }
  273. /* We are done here */
  274. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  275. }
  276. spin_unlock_irqrestore(&dwc->lock, flags);
  277. dwc_complete_all(dw, dwc);
  278. return;
  279. }
  280. if (list_empty(&dwc->active_list)) {
  281. spin_unlock_irqrestore(&dwc->lock, flags);
  282. return;
  283. }
  284. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  285. dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
  286. spin_unlock_irqrestore(&dwc->lock, flags);
  287. return;
  288. }
  289. dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
  290. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  291. /* Initial residue value */
  292. desc->residue = desc->total_len;
  293. /* Check first descriptors addr */
  294. if (desc->txd.phys == DWC_LLP_LOC(llp)) {
  295. spin_unlock_irqrestore(&dwc->lock, flags);
  296. return;
  297. }
  298. /* Check first descriptors llp */
  299. if (lli_read(desc, llp) == llp) {
  300. /* This one is currently in progress */
  301. desc->residue -= dwc_get_sent(dwc);
  302. spin_unlock_irqrestore(&dwc->lock, flags);
  303. return;
  304. }
  305. desc->residue -= desc->len;
  306. list_for_each_entry(child, &desc->tx_list, desc_node) {
  307. if (lli_read(child, llp) == llp) {
  308. /* Currently in progress */
  309. desc->residue -= dwc_get_sent(dwc);
  310. spin_unlock_irqrestore(&dwc->lock, flags);
  311. return;
  312. }
  313. desc->residue -= child->len;
  314. }
  315. /*
  316. * No descriptors so far seem to be in progress, i.e.
  317. * this one must be done.
  318. */
  319. spin_unlock_irqrestore(&dwc->lock, flags);
  320. dwc_descriptor_complete(dwc, desc, true);
  321. spin_lock_irqsave(&dwc->lock, flags);
  322. }
  323. dev_err(chan2dev(&dwc->chan),
  324. "BUG: All descriptors done, but channel not idle!\n");
  325. /* Try to continue after resetting the channel... */
  326. dwc_chan_disable(dw, dwc);
  327. dwc_dostart_first_queued(dwc);
  328. spin_unlock_irqrestore(&dwc->lock, flags);
  329. }
  330. static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_desc *desc)
  331. {
  332. dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  333. lli_read(desc, sar),
  334. lli_read(desc, dar),
  335. lli_read(desc, llp),
  336. lli_read(desc, ctlhi),
  337. lli_read(desc, ctllo));
  338. }
  339. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  340. {
  341. struct dw_desc *bad_desc;
  342. struct dw_desc *child;
  343. unsigned long flags;
  344. dwc_scan_descriptors(dw, dwc);
  345. spin_lock_irqsave(&dwc->lock, flags);
  346. /*
  347. * The descriptor currently at the head of the active list is
  348. * borked. Since we don't have any way to report errors, we'll
  349. * just have to scream loudly and try to carry on.
  350. */
  351. bad_desc = dwc_first_active(dwc);
  352. list_del_init(&bad_desc->desc_node);
  353. list_move(dwc->queue.next, dwc->active_list.prev);
  354. /* Clear the error flag and try to restart the controller */
  355. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  356. if (!list_empty(&dwc->active_list))
  357. dwc_dostart(dwc, dwc_first_active(dwc));
  358. /*
  359. * WARN may seem harsh, but since this only happens
  360. * when someone submits a bad physical address in a
  361. * descriptor, we should consider ourselves lucky that the
  362. * controller flagged an error instead of scribbling over
  363. * random memory locations.
  364. */
  365. dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
  366. " cookie: %d\n", bad_desc->txd.cookie);
  367. dwc_dump_lli(dwc, bad_desc);
  368. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  369. dwc_dump_lli(dwc, child);
  370. spin_unlock_irqrestore(&dwc->lock, flags);
  371. /* Pretend the descriptor completed successfully */
  372. dwc_descriptor_complete(dwc, bad_desc, true);
  373. }
  374. static void dw_dma_tasklet(struct tasklet_struct *t)
  375. {
  376. struct dw_dma *dw = from_tasklet(dw, t, tasklet);
  377. struct dw_dma_chan *dwc;
  378. u32 status_xfer;
  379. u32 status_err;
  380. unsigned int i;
  381. status_xfer = dma_readl(dw, RAW.XFER);
  382. status_err = dma_readl(dw, RAW.ERROR);
  383. dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
  384. for (i = 0; i < dw->dma.chancnt; i++) {
  385. dwc = &dw->chan[i];
  386. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  387. dev_vdbg(dw->dma.dev, "Cyclic xfer is not implemented\n");
  388. else if (status_err & (1 << i))
  389. dwc_handle_error(dw, dwc);
  390. else if (status_xfer & (1 << i))
  391. dwc_scan_descriptors(dw, dwc);
  392. }
  393. /* Re-enable interrupts */
  394. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  395. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  396. }
  397. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  398. {
  399. struct dw_dma *dw = dev_id;
  400. u32 status;
  401. /* Check if we have any interrupt from the DMAC which is not in use */
  402. if (!dw->in_use)
  403. return IRQ_NONE;
  404. status = dma_readl(dw, STATUS_INT);
  405. dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
  406. /* Check if we have any interrupt from the DMAC */
  407. if (!status)
  408. return IRQ_NONE;
  409. /*
  410. * Just disable the interrupts. We'll turn them back on in the
  411. * softirq handler.
  412. */
  413. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  414. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  415. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  416. status = dma_readl(dw, STATUS_INT);
  417. if (status) {
  418. dev_err(dw->dma.dev,
  419. "BUG: Unexpected interrupts pending: 0x%x\n",
  420. status);
  421. /* Try to recover */
  422. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  423. channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
  424. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  425. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  426. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  427. }
  428. tasklet_schedule(&dw->tasklet);
  429. return IRQ_HANDLED;
  430. }
  431. /*----------------------------------------------------------------------*/
  432. static struct dma_async_tx_descriptor *
  433. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  434. size_t len, unsigned long flags)
  435. {
  436. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  437. struct dw_dma *dw = to_dw_dma(chan->device);
  438. struct dw_desc *desc;
  439. struct dw_desc *first;
  440. struct dw_desc *prev;
  441. size_t xfer_count;
  442. size_t offset;
  443. u8 m_master = dwc->dws.m_master;
  444. unsigned int src_width;
  445. unsigned int dst_width;
  446. unsigned int data_width = dw->pdata->data_width[m_master];
  447. u32 ctllo, ctlhi;
  448. u8 lms = DWC_LLP_LMS(m_master);
  449. dev_vdbg(chan2dev(chan),
  450. "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
  451. &dest, &src, len, flags);
  452. if (unlikely(!len)) {
  453. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  454. return NULL;
  455. }
  456. dwc->direction = DMA_MEM_TO_MEM;
  457. src_width = dst_width = __ffs(data_width | src | dest | len);
  458. ctllo = dw->prepare_ctllo(dwc)
  459. | DWC_CTLL_DST_WIDTH(dst_width)
  460. | DWC_CTLL_SRC_WIDTH(src_width)
  461. | DWC_CTLL_DST_INC
  462. | DWC_CTLL_SRC_INC
  463. | DWC_CTLL_FC_M2M;
  464. prev = first = NULL;
  465. for (offset = 0; offset < len; offset += xfer_count) {
  466. desc = dwc_desc_get(dwc);
  467. if (!desc)
  468. goto err_desc_get;
  469. ctlhi = dw->bytes2block(dwc, len - offset, src_width, &xfer_count);
  470. lli_write(desc, sar, src + offset);
  471. lli_write(desc, dar, dest + offset);
  472. lli_write(desc, ctllo, ctllo);
  473. lli_write(desc, ctlhi, ctlhi);
  474. desc->len = xfer_count;
  475. if (!first) {
  476. first = desc;
  477. } else {
  478. lli_write(prev, llp, desc->txd.phys | lms);
  479. list_add_tail(&desc->desc_node, &first->tx_list);
  480. }
  481. prev = desc;
  482. }
  483. if (flags & DMA_PREP_INTERRUPT)
  484. /* Trigger interrupt after last block */
  485. lli_set(prev, ctllo, DWC_CTLL_INT_EN);
  486. prev->lli.llp = 0;
  487. lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  488. first->txd.flags = flags;
  489. first->total_len = len;
  490. return &first->txd;
  491. err_desc_get:
  492. dwc_desc_put(dwc, first);
  493. return NULL;
  494. }
  495. static struct dma_async_tx_descriptor *
  496. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  497. unsigned int sg_len, enum dma_transfer_direction direction,
  498. unsigned long flags, void *context)
  499. {
  500. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  501. struct dw_dma *dw = to_dw_dma(chan->device);
  502. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  503. struct dw_desc *prev;
  504. struct dw_desc *first;
  505. u32 ctllo, ctlhi;
  506. u8 lms = DWC_LLP_LMS(dwc->dws.m_master);
  507. dma_addr_t reg;
  508. unsigned int reg_width;
  509. unsigned int mem_width;
  510. unsigned int i;
  511. struct scatterlist *sg;
  512. size_t total_len = 0;
  513. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  514. if (unlikely(!is_slave_direction(direction) || !sg_len))
  515. return NULL;
  516. dwc->direction = direction;
  517. prev = first = NULL;
  518. switch (direction) {
  519. case DMA_MEM_TO_DEV:
  520. reg_width = __ffs(sconfig->dst_addr_width);
  521. reg = sconfig->dst_addr;
  522. ctllo = dw->prepare_ctllo(dwc)
  523. | DWC_CTLL_DST_WIDTH(reg_width)
  524. | DWC_CTLL_DST_FIX
  525. | DWC_CTLL_SRC_INC;
  526. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  527. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  528. for_each_sg(sgl, sg, sg_len, i) {
  529. struct dw_desc *desc;
  530. u32 len, mem;
  531. size_t dlen;
  532. mem = sg_dma_address(sg);
  533. len = sg_dma_len(sg);
  534. mem_width = __ffs(sconfig->src_addr_width | mem | len);
  535. slave_sg_todev_fill_desc:
  536. desc = dwc_desc_get(dwc);
  537. if (!desc)
  538. goto err_desc_get;
  539. ctlhi = dw->bytes2block(dwc, len, mem_width, &dlen);
  540. lli_write(desc, sar, mem);
  541. lli_write(desc, dar, reg);
  542. lli_write(desc, ctlhi, ctlhi);
  543. lli_write(desc, ctllo, ctllo | DWC_CTLL_SRC_WIDTH(mem_width));
  544. desc->len = dlen;
  545. if (!first) {
  546. first = desc;
  547. } else {
  548. lli_write(prev, llp, desc->txd.phys | lms);
  549. list_add_tail(&desc->desc_node, &first->tx_list);
  550. }
  551. prev = desc;
  552. mem += dlen;
  553. len -= dlen;
  554. total_len += dlen;
  555. if (len)
  556. goto slave_sg_todev_fill_desc;
  557. }
  558. break;
  559. case DMA_DEV_TO_MEM:
  560. reg_width = __ffs(sconfig->src_addr_width);
  561. reg = sconfig->src_addr;
  562. ctllo = dw->prepare_ctllo(dwc)
  563. | DWC_CTLL_SRC_WIDTH(reg_width)
  564. | DWC_CTLL_DST_INC
  565. | DWC_CTLL_SRC_FIX;
  566. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  567. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  568. for_each_sg(sgl, sg, sg_len, i) {
  569. struct dw_desc *desc;
  570. u32 len, mem;
  571. size_t dlen;
  572. mem = sg_dma_address(sg);
  573. len = sg_dma_len(sg);
  574. slave_sg_fromdev_fill_desc:
  575. desc = dwc_desc_get(dwc);
  576. if (!desc)
  577. goto err_desc_get;
  578. ctlhi = dw->bytes2block(dwc, len, reg_width, &dlen);
  579. lli_write(desc, sar, reg);
  580. lli_write(desc, dar, mem);
  581. lli_write(desc, ctlhi, ctlhi);
  582. mem_width = __ffs(sconfig->dst_addr_width | mem);
  583. lli_write(desc, ctllo, ctllo | DWC_CTLL_DST_WIDTH(mem_width));
  584. desc->len = dlen;
  585. if (!first) {
  586. first = desc;
  587. } else {
  588. lli_write(prev, llp, desc->txd.phys | lms);
  589. list_add_tail(&desc->desc_node, &first->tx_list);
  590. }
  591. prev = desc;
  592. mem += dlen;
  593. len -= dlen;
  594. total_len += dlen;
  595. if (len)
  596. goto slave_sg_fromdev_fill_desc;
  597. }
  598. break;
  599. default:
  600. return NULL;
  601. }
  602. if (flags & DMA_PREP_INTERRUPT)
  603. /* Trigger interrupt after last block */
  604. lli_set(prev, ctllo, DWC_CTLL_INT_EN);
  605. prev->lli.llp = 0;
  606. lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  607. first->total_len = total_len;
  608. return &first->txd;
  609. err_desc_get:
  610. dev_err(chan2dev(chan),
  611. "not enough descriptors available. Direction %d\n", direction);
  612. dwc_desc_put(dwc, first);
  613. return NULL;
  614. }
  615. bool dw_dma_filter(struct dma_chan *chan, void *param)
  616. {
  617. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  618. struct dw_dma_slave *dws = param;
  619. if (dws->dma_dev != chan->device->dev)
  620. return false;
  621. /* permit channels in accordance with the channels mask */
  622. if (dws->channels && !(dws->channels & dwc->mask))
  623. return false;
  624. /* We have to copy data since dws can be temporary storage */
  625. memcpy(&dwc->dws, dws, sizeof(struct dw_dma_slave));
  626. return true;
  627. }
  628. EXPORT_SYMBOL_GPL(dw_dma_filter);
  629. static int dwc_verify_maxburst(struct dma_chan *chan)
  630. {
  631. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  632. dwc->dma_sconfig.src_maxburst =
  633. clamp(dwc->dma_sconfig.src_maxburst, 1U, dwc->max_burst);
  634. dwc->dma_sconfig.dst_maxburst =
  635. clamp(dwc->dma_sconfig.dst_maxburst, 1U, dwc->max_burst);
  636. dwc->dma_sconfig.src_maxburst =
  637. rounddown_pow_of_two(dwc->dma_sconfig.src_maxburst);
  638. dwc->dma_sconfig.dst_maxburst =
  639. rounddown_pow_of_two(dwc->dma_sconfig.dst_maxburst);
  640. return 0;
  641. }
  642. static int dwc_verify_p_buswidth(struct dma_chan *chan)
  643. {
  644. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  645. struct dw_dma *dw = to_dw_dma(chan->device);
  646. u32 reg_width, max_width;
  647. if (dwc->dma_sconfig.direction == DMA_MEM_TO_DEV)
  648. reg_width = dwc->dma_sconfig.dst_addr_width;
  649. else if (dwc->dma_sconfig.direction == DMA_DEV_TO_MEM)
  650. reg_width = dwc->dma_sconfig.src_addr_width;
  651. else /* DMA_MEM_TO_MEM */
  652. return 0;
  653. max_width = dw->pdata->data_width[dwc->dws.p_master];
  654. /* Fall-back to 1-byte transfer width if undefined */
  655. if (reg_width == DMA_SLAVE_BUSWIDTH_UNDEFINED)
  656. reg_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  657. else if (!is_power_of_2(reg_width) || reg_width > max_width)
  658. return -EINVAL;
  659. else /* bus width is valid */
  660. return 0;
  661. /* Update undefined addr width value */
  662. if (dwc->dma_sconfig.direction == DMA_MEM_TO_DEV)
  663. dwc->dma_sconfig.dst_addr_width = reg_width;
  664. else /* DMA_DEV_TO_MEM */
  665. dwc->dma_sconfig.src_addr_width = reg_width;
  666. return 0;
  667. }
  668. static int dwc_verify_m_buswidth(struct dma_chan *chan)
  669. {
  670. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  671. struct dw_dma *dw = to_dw_dma(chan->device);
  672. u32 reg_width, reg_burst, mem_width;
  673. mem_width = dw->pdata->data_width[dwc->dws.m_master];
  674. /*
  675. * It's possible to have a data portion locked in the DMA FIFO in case
  676. * of the channel suspension. Subsequent channel disabling will cause
  677. * that data silent loss. In order to prevent that maintain the src and
  678. * dst transfer widths coherency by means of the relation:
  679. * (CTLx.SRC_TR_WIDTH * CTLx.SRC_MSIZE >= CTLx.DST_TR_WIDTH)
  680. * Look for the details in the commit message that brings this change.
  681. *
  682. * Note the DMA configs utilized in the calculations below must have
  683. * been verified to have correct values by this method call.
  684. */
  685. if (dwc->dma_sconfig.direction == DMA_MEM_TO_DEV) {
  686. reg_width = dwc->dma_sconfig.dst_addr_width;
  687. if (mem_width < reg_width)
  688. return -EINVAL;
  689. dwc->dma_sconfig.src_addr_width = mem_width;
  690. } else if (dwc->dma_sconfig.direction == DMA_DEV_TO_MEM) {
  691. reg_width = dwc->dma_sconfig.src_addr_width;
  692. reg_burst = dwc->dma_sconfig.src_maxburst;
  693. dwc->dma_sconfig.dst_addr_width = min(mem_width, reg_width * reg_burst);
  694. }
  695. return 0;
  696. }
  697. static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
  698. {
  699. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  700. int ret;
  701. memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
  702. ret = dwc_verify_maxburst(chan);
  703. if (ret)
  704. return ret;
  705. ret = dwc_verify_p_buswidth(chan);
  706. if (ret)
  707. return ret;
  708. ret = dwc_verify_m_buswidth(chan);
  709. if (ret)
  710. return ret;
  711. return 0;
  712. }
  713. static void dwc_chan_pause(struct dw_dma_chan *dwc, bool drain)
  714. {
  715. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  716. unsigned int count = 20; /* timeout iterations */
  717. dw->suspend_chan(dwc, drain);
  718. while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
  719. udelay(2);
  720. set_bit(DW_DMA_IS_PAUSED, &dwc->flags);
  721. }
  722. static int dwc_pause(struct dma_chan *chan)
  723. {
  724. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  725. unsigned long flags;
  726. spin_lock_irqsave(&dwc->lock, flags);
  727. dwc_chan_pause(dwc, false);
  728. spin_unlock_irqrestore(&dwc->lock, flags);
  729. return 0;
  730. }
  731. static inline void dwc_chan_resume(struct dw_dma_chan *dwc, bool drain)
  732. {
  733. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  734. dw->resume_chan(dwc, drain);
  735. clear_bit(DW_DMA_IS_PAUSED, &dwc->flags);
  736. }
  737. static int dwc_resume(struct dma_chan *chan)
  738. {
  739. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  740. unsigned long flags;
  741. spin_lock_irqsave(&dwc->lock, flags);
  742. if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags))
  743. dwc_chan_resume(dwc, false);
  744. spin_unlock_irqrestore(&dwc->lock, flags);
  745. return 0;
  746. }
  747. static int dwc_terminate_all(struct dma_chan *chan)
  748. {
  749. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  750. struct dw_dma *dw = to_dw_dma(chan->device);
  751. struct dw_desc *desc, *_desc;
  752. unsigned long flags;
  753. LIST_HEAD(list);
  754. spin_lock_irqsave(&dwc->lock, flags);
  755. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  756. dwc_chan_pause(dwc, true);
  757. dwc_chan_disable(dw, dwc);
  758. dwc_chan_resume(dwc, true);
  759. /* active_list entries will end up before queued entries */
  760. list_splice_init(&dwc->queue, &list);
  761. list_splice_init(&dwc->active_list, &list);
  762. spin_unlock_irqrestore(&dwc->lock, flags);
  763. /* Flush all pending and queued descriptors */
  764. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  765. dwc_descriptor_complete(dwc, desc, false);
  766. return 0;
  767. }
  768. static struct dw_desc *dwc_find_desc(struct dw_dma_chan *dwc, dma_cookie_t c)
  769. {
  770. struct dw_desc *desc;
  771. list_for_each_entry(desc, &dwc->active_list, desc_node)
  772. if (desc->txd.cookie == c)
  773. return desc;
  774. return NULL;
  775. }
  776. static u32 dwc_get_residue_and_status(struct dw_dma_chan *dwc, dma_cookie_t cookie,
  777. enum dma_status *status)
  778. {
  779. struct dw_desc *desc;
  780. unsigned long flags;
  781. u32 residue;
  782. spin_lock_irqsave(&dwc->lock, flags);
  783. desc = dwc_find_desc(dwc, cookie);
  784. if (desc) {
  785. if (desc == dwc_first_active(dwc)) {
  786. residue = desc->residue;
  787. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
  788. residue -= dwc_get_sent(dwc);
  789. if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags))
  790. *status = DMA_PAUSED;
  791. } else {
  792. residue = desc->total_len;
  793. }
  794. } else {
  795. residue = 0;
  796. }
  797. spin_unlock_irqrestore(&dwc->lock, flags);
  798. return residue;
  799. }
  800. static enum dma_status
  801. dwc_tx_status(struct dma_chan *chan,
  802. dma_cookie_t cookie,
  803. struct dma_tx_state *txstate)
  804. {
  805. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  806. enum dma_status ret;
  807. ret = dma_cookie_status(chan, cookie, txstate);
  808. if (ret == DMA_COMPLETE)
  809. return ret;
  810. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  811. ret = dma_cookie_status(chan, cookie, txstate);
  812. if (ret == DMA_COMPLETE)
  813. return ret;
  814. dma_set_residue(txstate, dwc_get_residue_and_status(dwc, cookie, &ret));
  815. return ret;
  816. }
  817. static void dwc_issue_pending(struct dma_chan *chan)
  818. {
  819. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  820. unsigned long flags;
  821. spin_lock_irqsave(&dwc->lock, flags);
  822. if (list_empty(&dwc->active_list))
  823. dwc_dostart_first_queued(dwc);
  824. spin_unlock_irqrestore(&dwc->lock, flags);
  825. }
  826. /*----------------------------------------------------------------------*/
  827. void do_dw_dma_off(struct dw_dma *dw)
  828. {
  829. dma_writel(dw, CFG, 0);
  830. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  831. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  832. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  833. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  834. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  835. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  836. cpu_relax();
  837. }
  838. void do_dw_dma_on(struct dw_dma *dw)
  839. {
  840. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  841. }
  842. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  843. {
  844. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  845. struct dw_dma *dw = to_dw_dma(chan->device);
  846. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  847. /* ASSERT: channel is idle */
  848. if (dma_readl(dw, CH_EN) & dwc->mask) {
  849. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  850. return -EIO;
  851. }
  852. dma_cookie_init(chan);
  853. /*
  854. * NOTE: some controllers may have additional features that we
  855. * need to initialize here, like "scatter-gather" (which
  856. * doesn't mean what you think it means), and status writeback.
  857. */
  858. /*
  859. * We need controller-specific data to set up slave transfers.
  860. */
  861. if (chan->private && !dw_dma_filter(chan, chan->private)) {
  862. dev_warn(chan2dev(chan), "Wrong controller-specific data\n");
  863. return -EINVAL;
  864. }
  865. /* Enable controller here if needed */
  866. if (!dw->in_use)
  867. do_dw_dma_on(dw);
  868. dw->in_use |= dwc->mask;
  869. return 0;
  870. }
  871. static void dwc_free_chan_resources(struct dma_chan *chan)
  872. {
  873. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  874. struct dw_dma *dw = to_dw_dma(chan->device);
  875. unsigned long flags;
  876. dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
  877. dwc->descs_allocated);
  878. /* ASSERT: channel is idle */
  879. BUG_ON(!list_empty(&dwc->active_list));
  880. BUG_ON(!list_empty(&dwc->queue));
  881. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  882. spin_lock_irqsave(&dwc->lock, flags);
  883. /* Clear custom channel configuration */
  884. memset(&dwc->dws, 0, sizeof(struct dw_dma_slave));
  885. /* Disable interrupts */
  886. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  887. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  888. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  889. spin_unlock_irqrestore(&dwc->lock, flags);
  890. /* Disable controller in case it was a last user */
  891. dw->in_use &= ~dwc->mask;
  892. if (!dw->in_use)
  893. do_dw_dma_off(dw);
  894. dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
  895. }
  896. static void dwc_caps(struct dma_chan *chan, struct dma_slave_caps *caps)
  897. {
  898. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  899. caps->max_burst = dwc->max_burst;
  900. /*
  901. * It might be crucial for some devices to have the hardware
  902. * accelerated multi-block transfers supported, aka LLPs in DW DMAC
  903. * notation. So if LLPs are supported then max_sg_burst is set to
  904. * zero which means unlimited number of SG entries can be handled in a
  905. * single DMA transaction, otherwise it's just one SG entry.
  906. */
  907. if (dwc->nollp)
  908. caps->max_sg_burst = 1;
  909. else
  910. caps->max_sg_burst = 0;
  911. }
  912. int do_dma_probe(struct dw_dma_chip *chip)
  913. {
  914. struct dw_dma *dw = chip->dw;
  915. struct dw_dma_platform_data *pdata;
  916. bool autocfg = false;
  917. unsigned int dw_params;
  918. unsigned int i;
  919. int ret;
  920. dw->pdata = devm_kzalloc(chip->dev, sizeof(*dw->pdata), GFP_KERNEL);
  921. if (!dw->pdata)
  922. return -ENOMEM;
  923. dw->regs = chip->regs;
  924. pm_runtime_get_sync(chip->dev);
  925. if (!chip->pdata) {
  926. dw_params = dma_readl(dw, DW_PARAMS);
  927. dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
  928. autocfg = dw_params >> DW_PARAMS_EN & 1;
  929. if (!autocfg) {
  930. ret = -EINVAL;
  931. goto err_pdata;
  932. }
  933. /* Reassign the platform data pointer */
  934. pdata = dw->pdata;
  935. /* Get hardware configuration parameters */
  936. pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1;
  937. pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
  938. for (i = 0; i < pdata->nr_masters; i++) {
  939. pdata->data_width[i] =
  940. 4 << (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3);
  941. }
  942. pdata->block_size = dma_readl(dw, MAX_BLK_SIZE);
  943. /* Fill platform data with the default values */
  944. pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
  945. pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
  946. } else if (chip->pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
  947. ret = -EINVAL;
  948. goto err_pdata;
  949. } else {
  950. memcpy(dw->pdata, chip->pdata, sizeof(*dw->pdata));
  951. /* Reassign the platform data pointer */
  952. pdata = dw->pdata;
  953. }
  954. dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan),
  955. GFP_KERNEL);
  956. if (!dw->chan) {
  957. ret = -ENOMEM;
  958. goto err_pdata;
  959. }
  960. /* Calculate all channel mask before DMA setup */
  961. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  962. /* Force dma off, just in case */
  963. dw->disable(dw);
  964. /* Device and instance ID for IRQ and DMA pool */
  965. dw->set_device_name(dw, chip->id);
  966. /* Create a pool of consistent memory blocks for hardware descriptors */
  967. dw->desc_pool = dmam_pool_create(dw->name, chip->dev,
  968. sizeof(struct dw_desc), 4, 0);
  969. if (!dw->desc_pool) {
  970. dev_err(chip->dev, "No memory for descriptors dma pool\n");
  971. ret = -ENOMEM;
  972. goto err_pdata;
  973. }
  974. tasklet_setup(&dw->tasklet, dw_dma_tasklet);
  975. ret = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
  976. dw->name, dw);
  977. if (ret)
  978. goto err_pdata;
  979. INIT_LIST_HEAD(&dw->dma.channels);
  980. for (i = 0; i < pdata->nr_channels; i++) {
  981. struct dw_dma_chan *dwc = &dw->chan[i];
  982. dwc->chan.device = &dw->dma;
  983. dma_cookie_init(&dwc->chan);
  984. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  985. list_add_tail(&dwc->chan.device_node,
  986. &dw->dma.channels);
  987. else
  988. list_add(&dwc->chan.device_node, &dw->dma.channels);
  989. /* 7 is highest priority & 0 is lowest. */
  990. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  991. dwc->priority = pdata->nr_channels - i - 1;
  992. else
  993. dwc->priority = i;
  994. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  995. spin_lock_init(&dwc->lock);
  996. dwc->mask = 1 << i;
  997. INIT_LIST_HEAD(&dwc->active_list);
  998. INIT_LIST_HEAD(&dwc->queue);
  999. channel_clear_bit(dw, CH_EN, dwc->mask);
  1000. dwc->direction = DMA_TRANS_NONE;
  1001. /* Hardware configuration */
  1002. if (autocfg) {
  1003. unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1;
  1004. void __iomem *addr = &__dw_regs(dw)->DWC_PARAMS[r];
  1005. unsigned int dwc_params = readl(addr);
  1006. dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
  1007. dwc_params);
  1008. /*
  1009. * Decode maximum block size for given channel. The
  1010. * stored 4 bit value represents blocks from 0x00 for 3
  1011. * up to 0x0a for 4095.
  1012. */
  1013. dwc->block_size =
  1014. (4 << ((pdata->block_size >> 4 * i) & 0xf)) - 1;
  1015. /*
  1016. * According to the DW DMA databook the true scatter-
  1017. * gether LLPs aren't available if either multi-block
  1018. * config is disabled (CHx_MULTI_BLK_EN == 0) or the
  1019. * LLP register is hard-coded to zeros
  1020. * (CHx_HC_LLP == 1).
  1021. */
  1022. dwc->nollp =
  1023. (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0 ||
  1024. (dwc_params >> DWC_PARAMS_HC_LLP & 0x1) == 1;
  1025. dwc->max_burst =
  1026. (0x4 << (dwc_params >> DWC_PARAMS_MSIZE & 0x7));
  1027. } else {
  1028. dwc->block_size = pdata->block_size;
  1029. dwc->nollp = !pdata->multi_block[i];
  1030. dwc->max_burst = pdata->max_burst[i] ?: DW_DMA_MAX_BURST;
  1031. }
  1032. }
  1033. /* Clear all interrupts on all channels. */
  1034. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1035. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1036. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1037. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1038. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1039. /* Set capabilities */
  1040. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1041. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1042. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1043. dw->dma.dev = chip->dev;
  1044. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1045. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1046. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1047. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1048. dw->dma.device_caps = dwc_caps;
  1049. dw->dma.device_config = dwc_config;
  1050. dw->dma.device_pause = dwc_pause;
  1051. dw->dma.device_resume = dwc_resume;
  1052. dw->dma.device_terminate_all = dwc_terminate_all;
  1053. dw->dma.device_tx_status = dwc_tx_status;
  1054. dw->dma.device_issue_pending = dwc_issue_pending;
  1055. /* DMA capabilities */
  1056. dw->dma.min_burst = DW_DMA_MIN_BURST;
  1057. dw->dma.max_burst = DW_DMA_MAX_BURST;
  1058. dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS;
  1059. dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS;
  1060. dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  1061. BIT(DMA_MEM_TO_MEM);
  1062. dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1063. /*
  1064. * For now there is no hardware with non uniform maximum block size
  1065. * across all of the device channels, so we set the maximum segment
  1066. * size as the block size found for the very first channel.
  1067. */
  1068. dma_set_max_seg_size(dw->dma.dev, dw->chan[0].block_size);
  1069. ret = dma_async_device_register(&dw->dma);
  1070. if (ret)
  1071. goto err_dma_register;
  1072. dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
  1073. pdata->nr_channels);
  1074. pm_runtime_put_sync_suspend(chip->dev);
  1075. return 0;
  1076. err_dma_register:
  1077. free_irq(chip->irq, dw);
  1078. err_pdata:
  1079. pm_runtime_put_sync_suspend(chip->dev);
  1080. return ret;
  1081. }
  1082. int do_dma_remove(struct dw_dma_chip *chip)
  1083. {
  1084. struct dw_dma *dw = chip->dw;
  1085. struct dw_dma_chan *dwc, *_dwc;
  1086. pm_runtime_get_sync(chip->dev);
  1087. do_dw_dma_off(dw);
  1088. dma_async_device_unregister(&dw->dma);
  1089. free_irq(chip->irq, dw);
  1090. tasklet_kill(&dw->tasklet);
  1091. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1092. chan.device_node) {
  1093. list_del(&dwc->chan.device_node);
  1094. channel_clear_bit(dw, CH_EN, dwc->mask);
  1095. }
  1096. pm_runtime_put_sync_suspend(chip->dev);
  1097. return 0;
  1098. }
  1099. int do_dw_dma_disable(struct dw_dma_chip *chip)
  1100. {
  1101. struct dw_dma *dw = chip->dw;
  1102. dw->disable(dw);
  1103. return 0;
  1104. }
  1105. EXPORT_SYMBOL_GPL(do_dw_dma_disable);
  1106. int do_dw_dma_enable(struct dw_dma_chip *chip)
  1107. {
  1108. struct dw_dma *dw = chip->dw;
  1109. dw->enable(dw);
  1110. return 0;
  1111. }
  1112. EXPORT_SYMBOL_GPL(do_dw_dma_enable);
  1113. MODULE_LICENSE("GPL v2");
  1114. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
  1115. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1116. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");