core.c 37 KB

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