core.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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_chan_idma32(struct dw_dma_chan *dwc)
  121. {
  122. u32 cfghi = 0;
  123. u32 cfglo = 0;
  124. /* Set default burst alignment */
  125. cfglo |= IDMA32C_CFGL_DST_BURST_ALIGN | IDMA32C_CFGL_SRC_BURST_ALIGN;
  126. /* Low 4 bits of the request lines */
  127. cfghi |= IDMA32C_CFGH_DST_PER(dwc->dws.dst_id & 0xf);
  128. cfghi |= IDMA32C_CFGH_SRC_PER(dwc->dws.src_id & 0xf);
  129. /* Request line extension (2 bits) */
  130. cfghi |= IDMA32C_CFGH_DST_PER_EXT(dwc->dws.dst_id >> 4 & 0x3);
  131. cfghi |= IDMA32C_CFGH_SRC_PER_EXT(dwc->dws.src_id >> 4 & 0x3);
  132. channel_writel(dwc, CFG_LO, cfglo);
  133. channel_writel(dwc, CFG_HI, cfghi);
  134. }
  135. static void dwc_initialize_chan_dw(struct dw_dma_chan *dwc)
  136. {
  137. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  138. u32 cfghi = DWC_CFGH_FIFO_MODE;
  139. u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
  140. bool hs_polarity = dwc->dws.hs_polarity;
  141. cfghi |= DWC_CFGH_DST_PER(dwc->dws.dst_id);
  142. cfghi |= DWC_CFGH_SRC_PER(dwc->dws.src_id);
  143. cfghi |= DWC_CFGH_PROTCTL(dw->pdata->protctl);
  144. /* Set polarity of handshake interface */
  145. cfglo |= hs_polarity ? DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL : 0;
  146. channel_writel(dwc, CFG_LO, cfglo);
  147. channel_writel(dwc, CFG_HI, cfghi);
  148. }
  149. static void dwc_initialize(struct dw_dma_chan *dwc)
  150. {
  151. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  152. if (test_bit(DW_DMA_IS_INITIALIZED, &dwc->flags))
  153. return;
  154. if (dw->pdata->is_idma32)
  155. dwc_initialize_chan_idma32(dwc);
  156. else
  157. dwc_initialize_chan_dw(dwc);
  158. /* Enable interrupts */
  159. channel_set_bit(dw, MASK.XFER, dwc->mask);
  160. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  161. set_bit(DW_DMA_IS_INITIALIZED, &dwc->flags);
  162. }
  163. /*----------------------------------------------------------------------*/
  164. static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
  165. {
  166. dev_err(chan2dev(&dwc->chan),
  167. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  168. channel_readl(dwc, SAR),
  169. channel_readl(dwc, DAR),
  170. channel_readl(dwc, LLP),
  171. channel_readl(dwc, CTL_HI),
  172. channel_readl(dwc, CTL_LO));
  173. }
  174. static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
  175. {
  176. channel_clear_bit(dw, CH_EN, dwc->mask);
  177. while (dma_readl(dw, CH_EN) & dwc->mask)
  178. cpu_relax();
  179. }
  180. static u32 bytes2block(struct dw_dma_chan *dwc, size_t bytes,
  181. unsigned int width, size_t *len)
  182. {
  183. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  184. u32 block;
  185. /* Always in bytes for iDMA 32-bit */
  186. if (dw->pdata->is_idma32)
  187. width = 0;
  188. if ((bytes >> width) > dwc->block_size) {
  189. block = dwc->block_size;
  190. *len = block << width;
  191. } else {
  192. block = bytes >> width;
  193. *len = bytes;
  194. }
  195. return block;
  196. }
  197. static size_t block2bytes(struct dw_dma_chan *dwc, u32 block, u32 width)
  198. {
  199. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  200. if (dw->pdata->is_idma32)
  201. return IDMA32C_CTLH_BLOCK_TS(block);
  202. return DWC_CTLH_BLOCK_TS(block) << width;
  203. }
  204. /*----------------------------------------------------------------------*/
  205. /* Perform single block transfer */
  206. static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
  207. struct dw_desc *desc)
  208. {
  209. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  210. u32 ctllo;
  211. /*
  212. * Software emulation of LLP mode relies on interrupts to continue
  213. * multi block transfer.
  214. */
  215. ctllo = lli_read(desc, ctllo) | DWC_CTLL_INT_EN;
  216. channel_writel(dwc, SAR, lli_read(desc, sar));
  217. channel_writel(dwc, DAR, lli_read(desc, dar));
  218. channel_writel(dwc, CTL_LO, ctllo);
  219. channel_writel(dwc, CTL_HI, lli_read(desc, ctlhi));
  220. channel_set_bit(dw, CH_EN, dwc->mask);
  221. /* Move pointer to next descriptor */
  222. dwc->tx_node_active = dwc->tx_node_active->next;
  223. }
  224. /* Called with dwc->lock held and bh disabled */
  225. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  226. {
  227. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  228. u8 lms = DWC_LLP_LMS(dwc->dws.m_master);
  229. unsigned long was_soft_llp;
  230. /* ASSERT: channel is idle */
  231. if (dma_readl(dw, CH_EN) & dwc->mask) {
  232. dev_err(chan2dev(&dwc->chan),
  233. "%s: BUG: Attempted to start non-idle channel\n",
  234. __func__);
  235. dwc_dump_chan_regs(dwc);
  236. /* The tasklet will hopefully advance the queue... */
  237. return;
  238. }
  239. if (dwc->nollp) {
  240. was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
  241. &dwc->flags);
  242. if (was_soft_llp) {
  243. dev_err(chan2dev(&dwc->chan),
  244. "BUG: Attempted to start new LLP transfer inside ongoing one\n");
  245. return;
  246. }
  247. dwc_initialize(dwc);
  248. first->residue = first->total_len;
  249. dwc->tx_node_active = &first->tx_list;
  250. /* Submit first block */
  251. dwc_do_single_block(dwc, first);
  252. return;
  253. }
  254. dwc_initialize(dwc);
  255. channel_writel(dwc, LLP, first->txd.phys | lms);
  256. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  257. channel_writel(dwc, CTL_HI, 0);
  258. channel_set_bit(dw, CH_EN, dwc->mask);
  259. }
  260. static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
  261. {
  262. struct dw_desc *desc;
  263. if (list_empty(&dwc->queue))
  264. return;
  265. list_move(dwc->queue.next, &dwc->active_list);
  266. desc = dwc_first_active(dwc);
  267. dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
  268. dwc_dostart(dwc, desc);
  269. }
  270. /*----------------------------------------------------------------------*/
  271. static void
  272. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
  273. bool callback_required)
  274. {
  275. struct dma_async_tx_descriptor *txd = &desc->txd;
  276. struct dw_desc *child;
  277. unsigned long flags;
  278. struct dmaengine_desc_callback cb;
  279. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  280. spin_lock_irqsave(&dwc->lock, flags);
  281. dma_cookie_complete(txd);
  282. if (callback_required)
  283. dmaengine_desc_get_callback(txd, &cb);
  284. else
  285. memset(&cb, 0, sizeof(cb));
  286. /* async_tx_ack */
  287. list_for_each_entry(child, &desc->tx_list, desc_node)
  288. async_tx_ack(&child->txd);
  289. async_tx_ack(&desc->txd);
  290. dwc_desc_put(dwc, desc);
  291. spin_unlock_irqrestore(&dwc->lock, flags);
  292. dmaengine_desc_callback_invoke(&cb, NULL);
  293. }
  294. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  295. {
  296. struct dw_desc *desc, *_desc;
  297. LIST_HEAD(list);
  298. unsigned long flags;
  299. spin_lock_irqsave(&dwc->lock, flags);
  300. if (dma_readl(dw, CH_EN) & dwc->mask) {
  301. dev_err(chan2dev(&dwc->chan),
  302. "BUG: XFER bit set, but channel not idle!\n");
  303. /* Try to continue after resetting the channel... */
  304. dwc_chan_disable(dw, dwc);
  305. }
  306. /*
  307. * Submit queued descriptors ASAP, i.e. before we go through
  308. * the completed ones.
  309. */
  310. list_splice_init(&dwc->active_list, &list);
  311. dwc_dostart_first_queued(dwc);
  312. spin_unlock_irqrestore(&dwc->lock, flags);
  313. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  314. dwc_descriptor_complete(dwc, desc, true);
  315. }
  316. /* Returns how many bytes were already received from source */
  317. static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
  318. {
  319. u32 ctlhi = channel_readl(dwc, CTL_HI);
  320. u32 ctllo = channel_readl(dwc, CTL_LO);
  321. return block2bytes(dwc, ctlhi, ctllo >> 4 & 7);
  322. }
  323. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  324. {
  325. dma_addr_t llp;
  326. struct dw_desc *desc, *_desc;
  327. struct dw_desc *child;
  328. u32 status_xfer;
  329. unsigned long flags;
  330. spin_lock_irqsave(&dwc->lock, flags);
  331. llp = channel_readl(dwc, LLP);
  332. status_xfer = dma_readl(dw, RAW.XFER);
  333. if (status_xfer & dwc->mask) {
  334. /* Everything we've submitted is done */
  335. dma_writel(dw, CLEAR.XFER, dwc->mask);
  336. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  337. struct list_head *head, *active = dwc->tx_node_active;
  338. /*
  339. * We are inside first active descriptor.
  340. * Otherwise something is really wrong.
  341. */
  342. desc = dwc_first_active(dwc);
  343. head = &desc->tx_list;
  344. if (active != head) {
  345. /* Update residue to reflect last sent descriptor */
  346. if (active == head->next)
  347. desc->residue -= desc->len;
  348. else
  349. desc->residue -= to_dw_desc(active->prev)->len;
  350. child = to_dw_desc(active);
  351. /* Submit next block */
  352. dwc_do_single_block(dwc, child);
  353. spin_unlock_irqrestore(&dwc->lock, flags);
  354. return;
  355. }
  356. /* We are done here */
  357. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  358. }
  359. spin_unlock_irqrestore(&dwc->lock, flags);
  360. dwc_complete_all(dw, dwc);
  361. return;
  362. }
  363. if (list_empty(&dwc->active_list)) {
  364. spin_unlock_irqrestore(&dwc->lock, flags);
  365. return;
  366. }
  367. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
  368. dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
  369. spin_unlock_irqrestore(&dwc->lock, flags);
  370. return;
  371. }
  372. dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
  373. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  374. /* Initial residue value */
  375. desc->residue = desc->total_len;
  376. /* Check first descriptors addr */
  377. if (desc->txd.phys == DWC_LLP_LOC(llp)) {
  378. spin_unlock_irqrestore(&dwc->lock, flags);
  379. return;
  380. }
  381. /* Check first descriptors llp */
  382. if (lli_read(desc, llp) == llp) {
  383. /* This one is currently in progress */
  384. desc->residue -= dwc_get_sent(dwc);
  385. spin_unlock_irqrestore(&dwc->lock, flags);
  386. return;
  387. }
  388. desc->residue -= desc->len;
  389. list_for_each_entry(child, &desc->tx_list, desc_node) {
  390. if (lli_read(child, llp) == llp) {
  391. /* Currently in progress */
  392. desc->residue -= dwc_get_sent(dwc);
  393. spin_unlock_irqrestore(&dwc->lock, flags);
  394. return;
  395. }
  396. desc->residue -= child->len;
  397. }
  398. /*
  399. * No descriptors so far seem to be in progress, i.e.
  400. * this one must be done.
  401. */
  402. spin_unlock_irqrestore(&dwc->lock, flags);
  403. dwc_descriptor_complete(dwc, desc, true);
  404. spin_lock_irqsave(&dwc->lock, flags);
  405. }
  406. dev_err(chan2dev(&dwc->chan),
  407. "BUG: All descriptors done, but channel not idle!\n");
  408. /* Try to continue after resetting the channel... */
  409. dwc_chan_disable(dw, dwc);
  410. dwc_dostart_first_queued(dwc);
  411. spin_unlock_irqrestore(&dwc->lock, flags);
  412. }
  413. static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_desc *desc)
  414. {
  415. dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  416. lli_read(desc, sar),
  417. lli_read(desc, dar),
  418. lli_read(desc, llp),
  419. lli_read(desc, ctlhi),
  420. lli_read(desc, ctllo));
  421. }
  422. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  423. {
  424. struct dw_desc *bad_desc;
  425. struct dw_desc *child;
  426. unsigned long flags;
  427. dwc_scan_descriptors(dw, dwc);
  428. spin_lock_irqsave(&dwc->lock, flags);
  429. /*
  430. * The descriptor currently at the head of the active list is
  431. * borked. Since we don't have any way to report errors, we'll
  432. * just have to scream loudly and try to carry on.
  433. */
  434. bad_desc = dwc_first_active(dwc);
  435. list_del_init(&bad_desc->desc_node);
  436. list_move(dwc->queue.next, dwc->active_list.prev);
  437. /* Clear the error flag and try to restart the controller */
  438. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  439. if (!list_empty(&dwc->active_list))
  440. dwc_dostart(dwc, dwc_first_active(dwc));
  441. /*
  442. * WARN may seem harsh, but since this only happens
  443. * when someone submits a bad physical address in a
  444. * descriptor, we should consider ourselves lucky that the
  445. * controller flagged an error instead of scribbling over
  446. * random memory locations.
  447. */
  448. dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
  449. " cookie: %d\n", bad_desc->txd.cookie);
  450. dwc_dump_lli(dwc, bad_desc);
  451. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  452. dwc_dump_lli(dwc, child);
  453. spin_unlock_irqrestore(&dwc->lock, flags);
  454. /* Pretend the descriptor completed successfully */
  455. dwc_descriptor_complete(dwc, bad_desc, true);
  456. }
  457. static void dw_dma_tasklet(unsigned long data)
  458. {
  459. struct dw_dma *dw = (struct dw_dma *)data;
  460. struct dw_dma_chan *dwc;
  461. u32 status_xfer;
  462. u32 status_err;
  463. unsigned int i;
  464. status_xfer = dma_readl(dw, RAW.XFER);
  465. status_err = dma_readl(dw, RAW.ERROR);
  466. dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
  467. for (i = 0; i < dw->dma.chancnt; i++) {
  468. dwc = &dw->chan[i];
  469. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  470. dev_vdbg(dw->dma.dev, "Cyclic xfer is not implemented\n");
  471. else if (status_err & (1 << i))
  472. dwc_handle_error(dw, dwc);
  473. else if (status_xfer & (1 << i))
  474. dwc_scan_descriptors(dw, dwc);
  475. }
  476. /* Re-enable interrupts */
  477. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  478. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  479. }
  480. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  481. {
  482. struct dw_dma *dw = dev_id;
  483. u32 status;
  484. /* Check if we have any interrupt from the DMAC which is not in use */
  485. if (!dw->in_use)
  486. return IRQ_NONE;
  487. status = dma_readl(dw, STATUS_INT);
  488. dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
  489. /* Check if we have any interrupt from the DMAC */
  490. if (!status)
  491. return IRQ_NONE;
  492. /*
  493. * Just disable the interrupts. We'll turn them back on in the
  494. * softirq handler.
  495. */
  496. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  497. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  498. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  499. status = dma_readl(dw, STATUS_INT);
  500. if (status) {
  501. dev_err(dw->dma.dev,
  502. "BUG: Unexpected interrupts pending: 0x%x\n",
  503. status);
  504. /* Try to recover */
  505. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  506. channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
  507. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  508. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  509. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  510. }
  511. tasklet_schedule(&dw->tasklet);
  512. return IRQ_HANDLED;
  513. }
  514. /*----------------------------------------------------------------------*/
  515. static struct dma_async_tx_descriptor *
  516. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  517. size_t len, unsigned long flags)
  518. {
  519. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  520. struct dw_dma *dw = to_dw_dma(chan->device);
  521. struct dw_desc *desc;
  522. struct dw_desc *first;
  523. struct dw_desc *prev;
  524. size_t xfer_count;
  525. size_t offset;
  526. u8 m_master = dwc->dws.m_master;
  527. unsigned int src_width;
  528. unsigned int dst_width;
  529. unsigned int data_width = dw->pdata->data_width[m_master];
  530. u32 ctllo;
  531. u8 lms = DWC_LLP_LMS(m_master);
  532. dev_vdbg(chan2dev(chan),
  533. "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
  534. &dest, &src, len, flags);
  535. if (unlikely(!len)) {
  536. dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
  537. return NULL;
  538. }
  539. dwc->direction = DMA_MEM_TO_MEM;
  540. src_width = dst_width = __ffs(data_width | src | dest | len);
  541. ctllo = DWC_DEFAULT_CTLLO(chan)
  542. | DWC_CTLL_DST_WIDTH(dst_width)
  543. | DWC_CTLL_SRC_WIDTH(src_width)
  544. | DWC_CTLL_DST_INC
  545. | DWC_CTLL_SRC_INC
  546. | DWC_CTLL_FC_M2M;
  547. prev = first = NULL;
  548. for (offset = 0; offset < len; offset += xfer_count) {
  549. desc = dwc_desc_get(dwc);
  550. if (!desc)
  551. goto err_desc_get;
  552. lli_write(desc, sar, src + offset);
  553. lli_write(desc, dar, dest + offset);
  554. lli_write(desc, ctllo, ctllo);
  555. lli_write(desc, ctlhi, bytes2block(dwc, len - offset, src_width, &xfer_count));
  556. desc->len = xfer_count;
  557. if (!first) {
  558. first = desc;
  559. } else {
  560. lli_write(prev, llp, desc->txd.phys | lms);
  561. list_add_tail(&desc->desc_node, &first->tx_list);
  562. }
  563. prev = desc;
  564. }
  565. if (flags & DMA_PREP_INTERRUPT)
  566. /* Trigger interrupt after last block */
  567. lli_set(prev, ctllo, DWC_CTLL_INT_EN);
  568. prev->lli.llp = 0;
  569. lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  570. first->txd.flags = flags;
  571. first->total_len = len;
  572. return &first->txd;
  573. err_desc_get:
  574. dwc_desc_put(dwc, first);
  575. return NULL;
  576. }
  577. static struct dma_async_tx_descriptor *
  578. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  579. unsigned int sg_len, enum dma_transfer_direction direction,
  580. unsigned long flags, void *context)
  581. {
  582. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  583. struct dw_dma *dw = to_dw_dma(chan->device);
  584. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  585. struct dw_desc *prev;
  586. struct dw_desc *first;
  587. u32 ctllo;
  588. u8 m_master = dwc->dws.m_master;
  589. u8 lms = DWC_LLP_LMS(m_master);
  590. dma_addr_t reg;
  591. unsigned int reg_width;
  592. unsigned int mem_width;
  593. unsigned int data_width = dw->pdata->data_width[m_master];
  594. unsigned int i;
  595. struct scatterlist *sg;
  596. size_t total_len = 0;
  597. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  598. if (unlikely(!is_slave_direction(direction) || !sg_len))
  599. return NULL;
  600. dwc->direction = direction;
  601. prev = first = NULL;
  602. switch (direction) {
  603. case DMA_MEM_TO_DEV:
  604. reg_width = __ffs(sconfig->dst_addr_width);
  605. reg = sconfig->dst_addr;
  606. ctllo = (DWC_DEFAULT_CTLLO(chan)
  607. | DWC_CTLL_DST_WIDTH(reg_width)
  608. | DWC_CTLL_DST_FIX
  609. | DWC_CTLL_SRC_INC);
  610. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  611. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  612. for_each_sg(sgl, sg, sg_len, i) {
  613. struct dw_desc *desc;
  614. u32 len, mem;
  615. size_t dlen;
  616. mem = sg_dma_address(sg);
  617. len = sg_dma_len(sg);
  618. mem_width = __ffs(data_width | mem | len);
  619. slave_sg_todev_fill_desc:
  620. desc = dwc_desc_get(dwc);
  621. if (!desc)
  622. goto err_desc_get;
  623. lli_write(desc, sar, mem);
  624. lli_write(desc, dar, reg);
  625. lli_write(desc, ctlhi, bytes2block(dwc, len, mem_width, &dlen));
  626. lli_write(desc, ctllo, ctllo | DWC_CTLL_SRC_WIDTH(mem_width));
  627. desc->len = dlen;
  628. if (!first) {
  629. first = desc;
  630. } else {
  631. lli_write(prev, llp, desc->txd.phys | lms);
  632. list_add_tail(&desc->desc_node, &first->tx_list);
  633. }
  634. prev = desc;
  635. mem += dlen;
  636. len -= dlen;
  637. total_len += dlen;
  638. if (len)
  639. goto slave_sg_todev_fill_desc;
  640. }
  641. break;
  642. case DMA_DEV_TO_MEM:
  643. reg_width = __ffs(sconfig->src_addr_width);
  644. reg = sconfig->src_addr;
  645. ctllo = (DWC_DEFAULT_CTLLO(chan)
  646. | DWC_CTLL_SRC_WIDTH(reg_width)
  647. | DWC_CTLL_DST_INC
  648. | DWC_CTLL_SRC_FIX);
  649. ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  650. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  651. for_each_sg(sgl, sg, sg_len, i) {
  652. struct dw_desc *desc;
  653. u32 len, mem;
  654. size_t dlen;
  655. mem = sg_dma_address(sg);
  656. len = sg_dma_len(sg);
  657. slave_sg_fromdev_fill_desc:
  658. desc = dwc_desc_get(dwc);
  659. if (!desc)
  660. goto err_desc_get;
  661. lli_write(desc, sar, reg);
  662. lli_write(desc, dar, mem);
  663. lli_write(desc, ctlhi, bytes2block(dwc, len, reg_width, &dlen));
  664. mem_width = __ffs(data_width | mem | dlen);
  665. lli_write(desc, ctllo, ctllo | DWC_CTLL_DST_WIDTH(mem_width));
  666. desc->len = dlen;
  667. if (!first) {
  668. first = desc;
  669. } else {
  670. lli_write(prev, llp, desc->txd.phys | lms);
  671. list_add_tail(&desc->desc_node, &first->tx_list);
  672. }
  673. prev = desc;
  674. mem += dlen;
  675. len -= dlen;
  676. total_len += dlen;
  677. if (len)
  678. goto slave_sg_fromdev_fill_desc;
  679. }
  680. break;
  681. default:
  682. return NULL;
  683. }
  684. if (flags & DMA_PREP_INTERRUPT)
  685. /* Trigger interrupt after last block */
  686. lli_set(prev, ctllo, DWC_CTLL_INT_EN);
  687. prev->lli.llp = 0;
  688. lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  689. first->total_len = total_len;
  690. return &first->txd;
  691. err_desc_get:
  692. dev_err(chan2dev(chan),
  693. "not enough descriptors available. Direction %d\n", direction);
  694. dwc_desc_put(dwc, first);
  695. return NULL;
  696. }
  697. bool dw_dma_filter(struct dma_chan *chan, void *param)
  698. {
  699. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  700. struct dw_dma_slave *dws = param;
  701. if (dws->dma_dev != chan->device->dev)
  702. return false;
  703. /* We have to copy data since dws can be temporary storage */
  704. memcpy(&dwc->dws, dws, sizeof(struct dw_dma_slave));
  705. return true;
  706. }
  707. EXPORT_SYMBOL_GPL(dw_dma_filter);
  708. static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
  709. {
  710. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  711. struct dma_slave_config *sc = &dwc->dma_sconfig;
  712. struct dw_dma *dw = to_dw_dma(chan->device);
  713. /*
  714. * Fix sconfig's burst size according to dw_dmac. We need to convert
  715. * them as:
  716. * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
  717. *
  718. * NOTE: burst size 2 is not supported by DesignWare controller.
  719. * iDMA 32-bit supports it.
  720. */
  721. u32 s = dw->pdata->is_idma32 ? 1 : 2;
  722. /* Check if chan will be configured for slave transfers */
  723. if (!is_slave_direction(sconfig->direction))
  724. return -EINVAL;
  725. memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
  726. dwc->direction = sconfig->direction;
  727. sc->src_maxburst = sc->src_maxburst > 1 ? fls(sc->src_maxburst) - s : 0;
  728. sc->dst_maxburst = sc->dst_maxburst > 1 ? fls(sc->dst_maxburst) - s : 0;
  729. return 0;
  730. }
  731. static void dwc_chan_pause(struct dw_dma_chan *dwc, bool drain)
  732. {
  733. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  734. unsigned int count = 20; /* timeout iterations */
  735. u32 cfglo;
  736. cfglo = channel_readl(dwc, CFG_LO);
  737. if (dw->pdata->is_idma32) {
  738. if (drain)
  739. cfglo |= IDMA32C_CFGL_CH_DRAIN;
  740. else
  741. cfglo &= ~IDMA32C_CFGL_CH_DRAIN;
  742. }
  743. channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
  744. while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
  745. udelay(2);
  746. set_bit(DW_DMA_IS_PAUSED, &dwc->flags);
  747. }
  748. static int dwc_pause(struct dma_chan *chan)
  749. {
  750. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  751. unsigned long flags;
  752. spin_lock_irqsave(&dwc->lock, flags);
  753. dwc_chan_pause(dwc, false);
  754. spin_unlock_irqrestore(&dwc->lock, flags);
  755. return 0;
  756. }
  757. static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
  758. {
  759. u32 cfglo = channel_readl(dwc, CFG_LO);
  760. channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
  761. clear_bit(DW_DMA_IS_PAUSED, &dwc->flags);
  762. }
  763. static int dwc_resume(struct dma_chan *chan)
  764. {
  765. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  766. unsigned long flags;
  767. spin_lock_irqsave(&dwc->lock, flags);
  768. if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags))
  769. dwc_chan_resume(dwc);
  770. spin_unlock_irqrestore(&dwc->lock, flags);
  771. return 0;
  772. }
  773. static int dwc_terminate_all(struct dma_chan *chan)
  774. {
  775. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  776. struct dw_dma *dw = to_dw_dma(chan->device);
  777. struct dw_desc *desc, *_desc;
  778. unsigned long flags;
  779. LIST_HEAD(list);
  780. spin_lock_irqsave(&dwc->lock, flags);
  781. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  782. dwc_chan_pause(dwc, true);
  783. dwc_chan_disable(dw, dwc);
  784. dwc_chan_resume(dwc);
  785. /* active_list entries will end up before queued entries */
  786. list_splice_init(&dwc->queue, &list);
  787. list_splice_init(&dwc->active_list, &list);
  788. spin_unlock_irqrestore(&dwc->lock, flags);
  789. /* Flush all pending and queued descriptors */
  790. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  791. dwc_descriptor_complete(dwc, desc, false);
  792. return 0;
  793. }
  794. static struct dw_desc *dwc_find_desc(struct dw_dma_chan *dwc, dma_cookie_t c)
  795. {
  796. struct dw_desc *desc;
  797. list_for_each_entry(desc, &dwc->active_list, desc_node)
  798. if (desc->txd.cookie == c)
  799. return desc;
  800. return NULL;
  801. }
  802. static u32 dwc_get_residue(struct dw_dma_chan *dwc, dma_cookie_t cookie)
  803. {
  804. struct dw_desc *desc;
  805. unsigned long flags;
  806. u32 residue;
  807. spin_lock_irqsave(&dwc->lock, flags);
  808. desc = dwc_find_desc(dwc, cookie);
  809. if (desc) {
  810. if (desc == dwc_first_active(dwc)) {
  811. residue = desc->residue;
  812. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
  813. residue -= dwc_get_sent(dwc);
  814. } else {
  815. residue = desc->total_len;
  816. }
  817. } else {
  818. residue = 0;
  819. }
  820. spin_unlock_irqrestore(&dwc->lock, flags);
  821. return residue;
  822. }
  823. static enum dma_status
  824. dwc_tx_status(struct dma_chan *chan,
  825. dma_cookie_t cookie,
  826. struct dma_tx_state *txstate)
  827. {
  828. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  829. enum dma_status ret;
  830. ret = dma_cookie_status(chan, cookie, txstate);
  831. if (ret == DMA_COMPLETE)
  832. return ret;
  833. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  834. ret = dma_cookie_status(chan, cookie, txstate);
  835. if (ret == DMA_COMPLETE)
  836. return ret;
  837. dma_set_residue(txstate, dwc_get_residue(dwc, cookie));
  838. if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags) && ret == DMA_IN_PROGRESS)
  839. return DMA_PAUSED;
  840. return ret;
  841. }
  842. static void dwc_issue_pending(struct dma_chan *chan)
  843. {
  844. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  845. unsigned long flags;
  846. spin_lock_irqsave(&dwc->lock, flags);
  847. if (list_empty(&dwc->active_list))
  848. dwc_dostart_first_queued(dwc);
  849. spin_unlock_irqrestore(&dwc->lock, flags);
  850. }
  851. /*----------------------------------------------------------------------*/
  852. /*
  853. * Program FIFO size of channels.
  854. *
  855. * By default full FIFO (512 bytes) is assigned to channel 0. Here we
  856. * slice FIFO on equal parts between channels.
  857. */
  858. static void idma32_fifo_partition(struct dw_dma *dw)
  859. {
  860. u64 value = IDMA32C_FP_PSIZE_CH0(64) | IDMA32C_FP_PSIZE_CH1(64) |
  861. IDMA32C_FP_UPDATE;
  862. u64 fifo_partition = 0;
  863. if (!dw->pdata->is_idma32)
  864. return;
  865. /* Fill FIFO_PARTITION low bits (Channels 0..1, 4..5) */
  866. fifo_partition |= value << 0;
  867. /* Fill FIFO_PARTITION high bits (Channels 2..3, 6..7) */
  868. fifo_partition |= value << 32;
  869. /* Program FIFO Partition registers - 64 bytes per channel */
  870. idma32_writeq(dw, FIFO_PARTITION1, fifo_partition);
  871. idma32_writeq(dw, FIFO_PARTITION0, fifo_partition);
  872. }
  873. static void dw_dma_off(struct dw_dma *dw)
  874. {
  875. unsigned int i;
  876. dma_writel(dw, CFG, 0);
  877. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  878. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  879. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  880. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  881. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  882. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  883. cpu_relax();
  884. for (i = 0; i < dw->dma.chancnt; i++)
  885. clear_bit(DW_DMA_IS_INITIALIZED, &dw->chan[i].flags);
  886. }
  887. static void dw_dma_on(struct dw_dma *dw)
  888. {
  889. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  890. }
  891. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  892. {
  893. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  894. struct dw_dma *dw = to_dw_dma(chan->device);
  895. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  896. /* ASSERT: channel is idle */
  897. if (dma_readl(dw, CH_EN) & dwc->mask) {
  898. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  899. return -EIO;
  900. }
  901. dma_cookie_init(chan);
  902. /*
  903. * NOTE: some controllers may have additional features that we
  904. * need to initialize here, like "scatter-gather" (which
  905. * doesn't mean what you think it means), and status writeback.
  906. */
  907. /*
  908. * We need controller-specific data to set up slave transfers.
  909. */
  910. if (chan->private && !dw_dma_filter(chan, chan->private)) {
  911. dev_warn(chan2dev(chan), "Wrong controller-specific data\n");
  912. return -EINVAL;
  913. }
  914. /* Enable controller here if needed */
  915. if (!dw->in_use)
  916. dw_dma_on(dw);
  917. dw->in_use |= dwc->mask;
  918. return 0;
  919. }
  920. static void dwc_free_chan_resources(struct dma_chan *chan)
  921. {
  922. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  923. struct dw_dma *dw = to_dw_dma(chan->device);
  924. unsigned long flags;
  925. LIST_HEAD(list);
  926. dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
  927. dwc->descs_allocated);
  928. /* ASSERT: channel is idle */
  929. BUG_ON(!list_empty(&dwc->active_list));
  930. BUG_ON(!list_empty(&dwc->queue));
  931. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  932. spin_lock_irqsave(&dwc->lock, flags);
  933. /* Clear custom channel configuration */
  934. memset(&dwc->dws, 0, sizeof(struct dw_dma_slave));
  935. clear_bit(DW_DMA_IS_INITIALIZED, &dwc->flags);
  936. /* Disable interrupts */
  937. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  938. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  939. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  940. spin_unlock_irqrestore(&dwc->lock, flags);
  941. /* Disable controller in case it was a last user */
  942. dw->in_use &= ~dwc->mask;
  943. if (!dw->in_use)
  944. dw_dma_off(dw);
  945. dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
  946. }
  947. int dw_dma_probe(struct dw_dma_chip *chip)
  948. {
  949. struct dw_dma_platform_data *pdata;
  950. struct dw_dma *dw;
  951. bool autocfg = false;
  952. unsigned int dw_params;
  953. unsigned int i;
  954. int err;
  955. dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
  956. if (!dw)
  957. return -ENOMEM;
  958. dw->pdata = devm_kzalloc(chip->dev, sizeof(*dw->pdata), GFP_KERNEL);
  959. if (!dw->pdata)
  960. return -ENOMEM;
  961. dw->regs = chip->regs;
  962. chip->dw = dw;
  963. pm_runtime_get_sync(chip->dev);
  964. if (!chip->pdata) {
  965. dw_params = dma_readl(dw, DW_PARAMS);
  966. dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
  967. autocfg = dw_params >> DW_PARAMS_EN & 1;
  968. if (!autocfg) {
  969. err = -EINVAL;
  970. goto err_pdata;
  971. }
  972. /* Reassign the platform data pointer */
  973. pdata = dw->pdata;
  974. /* Get hardware configuration parameters */
  975. pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1;
  976. pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
  977. for (i = 0; i < pdata->nr_masters; i++) {
  978. pdata->data_width[i] =
  979. 4 << (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3);
  980. }
  981. pdata->block_size = dma_readl(dw, MAX_BLK_SIZE);
  982. /* Fill platform data with the default values */
  983. pdata->is_private = true;
  984. pdata->is_memcpy = true;
  985. pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
  986. pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
  987. } else if (chip->pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
  988. err = -EINVAL;
  989. goto err_pdata;
  990. } else {
  991. memcpy(dw->pdata, chip->pdata, sizeof(*dw->pdata));
  992. /* Reassign the platform data pointer */
  993. pdata = dw->pdata;
  994. }
  995. dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan),
  996. GFP_KERNEL);
  997. if (!dw->chan) {
  998. err = -ENOMEM;
  999. goto err_pdata;
  1000. }
  1001. /* Calculate all channel mask before DMA setup */
  1002. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  1003. /* Force dma off, just in case */
  1004. dw_dma_off(dw);
  1005. idma32_fifo_partition(dw);
  1006. /* Device and instance ID for IRQ and DMA pool */
  1007. if (pdata->is_idma32)
  1008. snprintf(dw->name, sizeof(dw->name), "idma32:dmac%d", chip->id);
  1009. else
  1010. snprintf(dw->name, sizeof(dw->name), "dw:dmac%d", chip->id);
  1011. /* Create a pool of consistent memory blocks for hardware descriptors */
  1012. dw->desc_pool = dmam_pool_create(dw->name, chip->dev,
  1013. sizeof(struct dw_desc), 4, 0);
  1014. if (!dw->desc_pool) {
  1015. dev_err(chip->dev, "No memory for descriptors dma pool\n");
  1016. err = -ENOMEM;
  1017. goto err_pdata;
  1018. }
  1019. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1020. err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
  1021. dw->name, dw);
  1022. if (err)
  1023. goto err_pdata;
  1024. INIT_LIST_HEAD(&dw->dma.channels);
  1025. for (i = 0; i < pdata->nr_channels; i++) {
  1026. struct dw_dma_chan *dwc = &dw->chan[i];
  1027. dwc->chan.device = &dw->dma;
  1028. dma_cookie_init(&dwc->chan);
  1029. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  1030. list_add_tail(&dwc->chan.device_node,
  1031. &dw->dma.channels);
  1032. else
  1033. list_add(&dwc->chan.device_node, &dw->dma.channels);
  1034. /* 7 is highest priority & 0 is lowest. */
  1035. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  1036. dwc->priority = pdata->nr_channels - i - 1;
  1037. else
  1038. dwc->priority = i;
  1039. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1040. spin_lock_init(&dwc->lock);
  1041. dwc->mask = 1 << i;
  1042. INIT_LIST_HEAD(&dwc->active_list);
  1043. INIT_LIST_HEAD(&dwc->queue);
  1044. channel_clear_bit(dw, CH_EN, dwc->mask);
  1045. dwc->direction = DMA_TRANS_NONE;
  1046. /* Hardware configuration */
  1047. if (autocfg) {
  1048. unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1;
  1049. void __iomem *addr = &__dw_regs(dw)->DWC_PARAMS[r];
  1050. unsigned int dwc_params = readl(addr);
  1051. dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
  1052. dwc_params);
  1053. /*
  1054. * Decode maximum block size for given channel. The
  1055. * stored 4 bit value represents blocks from 0x00 for 3
  1056. * up to 0x0a for 4095.
  1057. */
  1058. dwc->block_size =
  1059. (4 << ((pdata->block_size >> 4 * i) & 0xf)) - 1;
  1060. dwc->nollp =
  1061. (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
  1062. } else {
  1063. dwc->block_size = pdata->block_size;
  1064. dwc->nollp = !pdata->multi_block[i];
  1065. }
  1066. }
  1067. /* Clear all interrupts on all channels. */
  1068. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1069. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1070. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1071. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1072. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1073. /* Set capabilities */
  1074. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1075. if (pdata->is_private)
  1076. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1077. if (pdata->is_memcpy)
  1078. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1079. dw->dma.dev = chip->dev;
  1080. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1081. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1082. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1083. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1084. dw->dma.device_config = dwc_config;
  1085. dw->dma.device_pause = dwc_pause;
  1086. dw->dma.device_resume = dwc_resume;
  1087. dw->dma.device_terminate_all = dwc_terminate_all;
  1088. dw->dma.device_tx_status = dwc_tx_status;
  1089. dw->dma.device_issue_pending = dwc_issue_pending;
  1090. /* DMA capabilities */
  1091. dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS;
  1092. dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS;
  1093. dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
  1094. BIT(DMA_MEM_TO_MEM);
  1095. dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
  1096. err = dma_async_device_register(&dw->dma);
  1097. if (err)
  1098. goto err_dma_register;
  1099. dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
  1100. pdata->nr_channels);
  1101. pm_runtime_put_sync_suspend(chip->dev);
  1102. return 0;
  1103. err_dma_register:
  1104. free_irq(chip->irq, dw);
  1105. err_pdata:
  1106. pm_runtime_put_sync_suspend(chip->dev);
  1107. return err;
  1108. }
  1109. EXPORT_SYMBOL_GPL(dw_dma_probe);
  1110. int dw_dma_remove(struct dw_dma_chip *chip)
  1111. {
  1112. struct dw_dma *dw = chip->dw;
  1113. struct dw_dma_chan *dwc, *_dwc;
  1114. pm_runtime_get_sync(chip->dev);
  1115. dw_dma_off(dw);
  1116. dma_async_device_unregister(&dw->dma);
  1117. free_irq(chip->irq, dw);
  1118. tasklet_kill(&dw->tasklet);
  1119. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1120. chan.device_node) {
  1121. list_del(&dwc->chan.device_node);
  1122. channel_clear_bit(dw, CH_EN, dwc->mask);
  1123. }
  1124. pm_runtime_put_sync_suspend(chip->dev);
  1125. return 0;
  1126. }
  1127. EXPORT_SYMBOL_GPL(dw_dma_remove);
  1128. int dw_dma_disable(struct dw_dma_chip *chip)
  1129. {
  1130. struct dw_dma *dw = chip->dw;
  1131. dw_dma_off(dw);
  1132. return 0;
  1133. }
  1134. EXPORT_SYMBOL_GPL(dw_dma_disable);
  1135. int dw_dma_enable(struct dw_dma_chip *chip)
  1136. {
  1137. struct dw_dma *dw = chip->dw;
  1138. idma32_fifo_partition(dw);
  1139. dw_dma_on(dw);
  1140. return 0;
  1141. }
  1142. EXPORT_SYMBOL_GPL(dw_dma_enable);
  1143. MODULE_LICENSE("GPL v2");
  1144. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
  1145. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1146. MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");