dw_dmac.c 48 KB

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