dw_dmac.c 48 KB

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