core.c 43 KB

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