dw_dmac.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955
  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 inline void convert_slave_id(struct dw_dma_chan *dwc)
  822. {
  823. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  824. dwc->dma_sconfig.slave_id -= dw->request_line_base;
  825. }
  826. static int
  827. set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
  828. {
  829. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  830. /* Check if chan will be configured for slave transfers */
  831. if (!is_slave_direction(sconfig->direction))
  832. return -EINVAL;
  833. memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
  834. dwc->direction = sconfig->direction;
  835. convert_burst(&dwc->dma_sconfig.src_maxburst);
  836. convert_burst(&dwc->dma_sconfig.dst_maxburst);
  837. convert_slave_id(dwc);
  838. return 0;
  839. }
  840. static inline void dwc_chan_pause(struct dw_dma_chan *dwc)
  841. {
  842. u32 cfglo = channel_readl(dwc, CFG_LO);
  843. channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
  844. while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY))
  845. cpu_relax();
  846. dwc->paused = true;
  847. }
  848. static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
  849. {
  850. u32 cfglo = channel_readl(dwc, CFG_LO);
  851. channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
  852. dwc->paused = false;
  853. }
  854. static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  855. unsigned long arg)
  856. {
  857. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  858. struct dw_dma *dw = to_dw_dma(chan->device);
  859. struct dw_desc *desc, *_desc;
  860. unsigned long flags;
  861. LIST_HEAD(list);
  862. if (cmd == DMA_PAUSE) {
  863. spin_lock_irqsave(&dwc->lock, flags);
  864. dwc_chan_pause(dwc);
  865. spin_unlock_irqrestore(&dwc->lock, flags);
  866. } else if (cmd == DMA_RESUME) {
  867. if (!dwc->paused)
  868. return 0;
  869. spin_lock_irqsave(&dwc->lock, flags);
  870. dwc_chan_resume(dwc);
  871. spin_unlock_irqrestore(&dwc->lock, flags);
  872. } else if (cmd == DMA_TERMINATE_ALL) {
  873. spin_lock_irqsave(&dwc->lock, flags);
  874. clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
  875. dwc_chan_disable(dw, dwc);
  876. dwc_chan_resume(dwc);
  877. /* active_list entries will end up before queued entries */
  878. list_splice_init(&dwc->queue, &list);
  879. list_splice_init(&dwc->active_list, &list);
  880. spin_unlock_irqrestore(&dwc->lock, flags);
  881. /* Flush all pending and queued descriptors */
  882. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  883. dwc_descriptor_complete(dwc, desc, false);
  884. } else if (cmd == DMA_SLAVE_CONFIG) {
  885. return set_runtime_config(chan, (struct dma_slave_config *)arg);
  886. } else {
  887. return -ENXIO;
  888. }
  889. return 0;
  890. }
  891. static inline u32 dwc_get_residue(struct dw_dma_chan *dwc)
  892. {
  893. unsigned long flags;
  894. u32 residue;
  895. spin_lock_irqsave(&dwc->lock, flags);
  896. residue = dwc->residue;
  897. if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
  898. residue -= dwc_get_sent(dwc);
  899. spin_unlock_irqrestore(&dwc->lock, flags);
  900. return residue;
  901. }
  902. static enum dma_status
  903. dwc_tx_status(struct dma_chan *chan,
  904. dma_cookie_t cookie,
  905. struct dma_tx_state *txstate)
  906. {
  907. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  908. enum dma_status ret;
  909. ret = dma_cookie_status(chan, cookie, txstate);
  910. if (ret != DMA_SUCCESS) {
  911. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  912. ret = dma_cookie_status(chan, cookie, txstate);
  913. }
  914. if (ret != DMA_SUCCESS)
  915. dma_set_residue(txstate, dwc_get_residue(dwc));
  916. if (dwc->paused)
  917. return DMA_PAUSED;
  918. return ret;
  919. }
  920. static void dwc_issue_pending(struct dma_chan *chan)
  921. {
  922. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  923. if (!list_empty(&dwc->queue))
  924. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  925. }
  926. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  927. {
  928. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  929. struct dw_dma *dw = to_dw_dma(chan->device);
  930. struct dw_desc *desc;
  931. int i;
  932. unsigned long flags;
  933. dev_vdbg(chan2dev(chan), "%s\n", __func__);
  934. /* ASSERT: channel is idle */
  935. if (dma_readl(dw, CH_EN) & dwc->mask) {
  936. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  937. return -EIO;
  938. }
  939. dma_cookie_init(chan);
  940. /*
  941. * NOTE: some controllers may have additional features that we
  942. * need to initialize here, like "scatter-gather" (which
  943. * doesn't mean what you think it means), and status writeback.
  944. */
  945. spin_lock_irqsave(&dwc->lock, flags);
  946. i = dwc->descs_allocated;
  947. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  948. dma_addr_t phys;
  949. spin_unlock_irqrestore(&dwc->lock, flags);
  950. desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
  951. if (!desc)
  952. goto err_desc_alloc;
  953. memset(desc, 0, sizeof(struct dw_desc));
  954. INIT_LIST_HEAD(&desc->tx_list);
  955. dma_async_tx_descriptor_init(&desc->txd, chan);
  956. desc->txd.tx_submit = dwc_tx_submit;
  957. desc->txd.flags = DMA_CTRL_ACK;
  958. desc->txd.phys = phys;
  959. dwc_desc_put(dwc, desc);
  960. spin_lock_irqsave(&dwc->lock, flags);
  961. i = ++dwc->descs_allocated;
  962. }
  963. spin_unlock_irqrestore(&dwc->lock, flags);
  964. dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
  965. return i;
  966. err_desc_alloc:
  967. dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
  968. return i;
  969. }
  970. static void dwc_free_chan_resources(struct dma_chan *chan)
  971. {
  972. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  973. struct dw_dma *dw = to_dw_dma(chan->device);
  974. struct dw_desc *desc, *_desc;
  975. unsigned long flags;
  976. LIST_HEAD(list);
  977. dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
  978. dwc->descs_allocated);
  979. /* ASSERT: channel is idle */
  980. BUG_ON(!list_empty(&dwc->active_list));
  981. BUG_ON(!list_empty(&dwc->queue));
  982. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  983. spin_lock_irqsave(&dwc->lock, flags);
  984. list_splice_init(&dwc->free_list, &list);
  985. dwc->descs_allocated = 0;
  986. dwc->initialized = false;
  987. /* Disable interrupts */
  988. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  989. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  990. spin_unlock_irqrestore(&dwc->lock, flags);
  991. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  992. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  993. dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
  994. }
  995. dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
  996. }
  997. struct dw_dma_filter_args {
  998. struct dw_dma *dw;
  999. unsigned int req;
  1000. unsigned int src;
  1001. unsigned int dst;
  1002. };
  1003. static bool dw_dma_generic_filter(struct dma_chan *chan, void *param)
  1004. {
  1005. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1006. struct dw_dma *dw = to_dw_dma(chan->device);
  1007. struct dw_dma_filter_args *fargs = param;
  1008. struct dw_dma_slave *dws = &dwc->slave;
  1009. /* ensure the device matches our channel */
  1010. if (chan->device != &fargs->dw->dma)
  1011. return false;
  1012. dws->dma_dev = dw->dma.dev;
  1013. dws->cfg_hi = ~0;
  1014. dws->cfg_lo = ~0;
  1015. dws->src_master = fargs->src;
  1016. dws->dst_master = fargs->dst;
  1017. dwc->request_line = fargs->req;
  1018. chan->private = dws;
  1019. return true;
  1020. }
  1021. static struct dma_chan *dw_dma_xlate(struct of_phandle_args *dma_spec,
  1022. struct of_dma *ofdma)
  1023. {
  1024. struct dw_dma *dw = ofdma->of_dma_data;
  1025. struct dw_dma_filter_args fargs = {
  1026. .dw = dw,
  1027. };
  1028. dma_cap_mask_t cap;
  1029. if (dma_spec->args_count != 3)
  1030. return NULL;
  1031. fargs.req = dma_spec->args[0];
  1032. fargs.src = dma_spec->args[1];
  1033. fargs.dst = dma_spec->args[2];
  1034. if (WARN_ON(fargs.req >= DW_DMA_MAX_NR_REQUESTS ||
  1035. fargs.src >= dw->nr_masters ||
  1036. fargs.dst >= dw->nr_masters))
  1037. return NULL;
  1038. dma_cap_zero(cap);
  1039. dma_cap_set(DMA_SLAVE, cap);
  1040. /* TODO: there should be a simpler way to do this */
  1041. return dma_request_channel(cap, dw_dma_generic_filter, &fargs);
  1042. }
  1043. /* --------------------- Cyclic DMA API extensions -------------------- */
  1044. /**
  1045. * dw_dma_cyclic_start - start the cyclic DMA transfer
  1046. * @chan: the DMA channel to start
  1047. *
  1048. * Must be called with soft interrupts disabled. Returns zero on success or
  1049. * -errno on failure.
  1050. */
  1051. int dw_dma_cyclic_start(struct dma_chan *chan)
  1052. {
  1053. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1054. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1055. unsigned long flags;
  1056. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  1057. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  1058. return -ENODEV;
  1059. }
  1060. spin_lock_irqsave(&dwc->lock, flags);
  1061. /* assert channel is idle */
  1062. if (dma_readl(dw, CH_EN) & dwc->mask) {
  1063. dev_err(chan2dev(&dwc->chan),
  1064. "BUG: Attempted to start non-idle channel\n");
  1065. dwc_dump_chan_regs(dwc);
  1066. spin_unlock_irqrestore(&dwc->lock, flags);
  1067. return -EBUSY;
  1068. }
  1069. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1070. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1071. /* setup DMAC channel registers */
  1072. channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
  1073. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  1074. channel_writel(dwc, CTL_HI, 0);
  1075. channel_set_bit(dw, CH_EN, dwc->mask);
  1076. spin_unlock_irqrestore(&dwc->lock, flags);
  1077. return 0;
  1078. }
  1079. EXPORT_SYMBOL(dw_dma_cyclic_start);
  1080. /**
  1081. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  1082. * @chan: the DMA channel to stop
  1083. *
  1084. * Must be called with soft interrupts disabled.
  1085. */
  1086. void dw_dma_cyclic_stop(struct dma_chan *chan)
  1087. {
  1088. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1089. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1090. unsigned long flags;
  1091. spin_lock_irqsave(&dwc->lock, flags);
  1092. dwc_chan_disable(dw, dwc);
  1093. spin_unlock_irqrestore(&dwc->lock, flags);
  1094. }
  1095. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  1096. /**
  1097. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  1098. * @chan: the DMA channel to prepare
  1099. * @buf_addr: physical DMA address where the buffer starts
  1100. * @buf_len: total number of bytes for the entire buffer
  1101. * @period_len: number of bytes for each period
  1102. * @direction: transfer direction, to or from device
  1103. *
  1104. * Must be called before trying to start the transfer. Returns a valid struct
  1105. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  1106. */
  1107. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  1108. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  1109. enum dma_transfer_direction direction)
  1110. {
  1111. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1112. struct dma_slave_config *sconfig = &dwc->dma_sconfig;
  1113. struct dw_cyclic_desc *cdesc;
  1114. struct dw_cyclic_desc *retval = NULL;
  1115. struct dw_desc *desc;
  1116. struct dw_desc *last = NULL;
  1117. unsigned long was_cyclic;
  1118. unsigned int reg_width;
  1119. unsigned int periods;
  1120. unsigned int i;
  1121. unsigned long flags;
  1122. spin_lock_irqsave(&dwc->lock, flags);
  1123. if (dwc->nollp) {
  1124. spin_unlock_irqrestore(&dwc->lock, flags);
  1125. dev_dbg(chan2dev(&dwc->chan),
  1126. "channel doesn't support LLP transfers\n");
  1127. return ERR_PTR(-EINVAL);
  1128. }
  1129. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  1130. spin_unlock_irqrestore(&dwc->lock, flags);
  1131. dev_dbg(chan2dev(&dwc->chan),
  1132. "queue and/or active list are not empty\n");
  1133. return ERR_PTR(-EBUSY);
  1134. }
  1135. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1136. spin_unlock_irqrestore(&dwc->lock, flags);
  1137. if (was_cyclic) {
  1138. dev_dbg(chan2dev(&dwc->chan),
  1139. "channel already prepared for cyclic DMA\n");
  1140. return ERR_PTR(-EBUSY);
  1141. }
  1142. retval = ERR_PTR(-EINVAL);
  1143. if (unlikely(!is_slave_direction(direction)))
  1144. goto out_err;
  1145. dwc->direction = direction;
  1146. if (direction == DMA_MEM_TO_DEV)
  1147. reg_width = __ffs(sconfig->dst_addr_width);
  1148. else
  1149. reg_width = __ffs(sconfig->src_addr_width);
  1150. periods = buf_len / period_len;
  1151. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  1152. if (period_len > (dwc->block_size << reg_width))
  1153. goto out_err;
  1154. if (unlikely(period_len & ((1 << reg_width) - 1)))
  1155. goto out_err;
  1156. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1157. goto out_err;
  1158. retval = ERR_PTR(-ENOMEM);
  1159. if (periods > NR_DESCS_PER_CHANNEL)
  1160. goto out_err;
  1161. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  1162. if (!cdesc)
  1163. goto out_err;
  1164. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  1165. if (!cdesc->desc)
  1166. goto out_err_alloc;
  1167. for (i = 0; i < periods; i++) {
  1168. desc = dwc_desc_get(dwc);
  1169. if (!desc)
  1170. goto out_err_desc_get;
  1171. switch (direction) {
  1172. case DMA_MEM_TO_DEV:
  1173. desc->lli.dar = sconfig->dst_addr;
  1174. desc->lli.sar = buf_addr + (period_len * i);
  1175. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
  1176. | DWC_CTLL_DST_WIDTH(reg_width)
  1177. | DWC_CTLL_SRC_WIDTH(reg_width)
  1178. | DWC_CTLL_DST_FIX
  1179. | DWC_CTLL_SRC_INC
  1180. | DWC_CTLL_INT_EN);
  1181. desc->lli.ctllo |= sconfig->device_fc ?
  1182. DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
  1183. DWC_CTLL_FC(DW_DMA_FC_D_M2P);
  1184. break;
  1185. case DMA_DEV_TO_MEM:
  1186. desc->lli.dar = buf_addr + (period_len * i);
  1187. desc->lli.sar = sconfig->src_addr;
  1188. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
  1189. | DWC_CTLL_SRC_WIDTH(reg_width)
  1190. | DWC_CTLL_DST_WIDTH(reg_width)
  1191. | DWC_CTLL_DST_INC
  1192. | DWC_CTLL_SRC_FIX
  1193. | DWC_CTLL_INT_EN);
  1194. desc->lli.ctllo |= sconfig->device_fc ?
  1195. DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
  1196. DWC_CTLL_FC(DW_DMA_FC_D_P2M);
  1197. break;
  1198. default:
  1199. break;
  1200. }
  1201. desc->lli.ctlhi = (period_len >> reg_width);
  1202. cdesc->desc[i] = desc;
  1203. if (last)
  1204. last->lli.llp = desc->txd.phys;
  1205. last = desc;
  1206. }
  1207. /* lets make a cyclic list */
  1208. last->lli.llp = cdesc->desc[0]->txd.phys;
  1209. dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%llx len %zu "
  1210. "period %zu periods %d\n", (unsigned long long)buf_addr,
  1211. buf_len, period_len, periods);
  1212. cdesc->periods = periods;
  1213. dwc->cdesc = cdesc;
  1214. return cdesc;
  1215. out_err_desc_get:
  1216. while (i--)
  1217. dwc_desc_put(dwc, cdesc->desc[i]);
  1218. out_err_alloc:
  1219. kfree(cdesc);
  1220. out_err:
  1221. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1222. return (struct dw_cyclic_desc *)retval;
  1223. }
  1224. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  1225. /**
  1226. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  1227. * @chan: the DMA channel to free
  1228. */
  1229. void dw_dma_cyclic_free(struct dma_chan *chan)
  1230. {
  1231. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1232. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1233. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1234. int i;
  1235. unsigned long flags;
  1236. dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
  1237. if (!cdesc)
  1238. return;
  1239. spin_lock_irqsave(&dwc->lock, flags);
  1240. dwc_chan_disable(dw, dwc);
  1241. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1242. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1243. spin_unlock_irqrestore(&dwc->lock, flags);
  1244. for (i = 0; i < cdesc->periods; i++)
  1245. dwc_desc_put(dwc, cdesc->desc[i]);
  1246. kfree(cdesc->desc);
  1247. kfree(cdesc);
  1248. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1249. }
  1250. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1251. /*----------------------------------------------------------------------*/
  1252. static void dw_dma_off(struct dw_dma *dw)
  1253. {
  1254. int i;
  1255. dma_writel(dw, CFG, 0);
  1256. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1257. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1258. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1259. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1260. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  1261. cpu_relax();
  1262. for (i = 0; i < dw->dma.chancnt; i++)
  1263. dw->chan[i].initialized = false;
  1264. }
  1265. #ifdef CONFIG_OF
  1266. static struct dw_dma_platform_data *
  1267. dw_dma_parse_dt(struct platform_device *pdev)
  1268. {
  1269. struct device_node *np = pdev->dev.of_node;
  1270. struct dw_dma_platform_data *pdata;
  1271. u32 tmp, arr[4];
  1272. if (!np) {
  1273. dev_err(&pdev->dev, "Missing DT data\n");
  1274. return NULL;
  1275. }
  1276. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1277. if (!pdata)
  1278. return NULL;
  1279. if (of_property_read_u32(np, "dma-channels", &pdata->nr_channels))
  1280. return NULL;
  1281. if (of_property_read_bool(np, "is_private"))
  1282. pdata->is_private = true;
  1283. if (!of_property_read_u32(np, "chan_allocation_order", &tmp))
  1284. pdata->chan_allocation_order = (unsigned char)tmp;
  1285. if (!of_property_read_u32(np, "chan_priority", &tmp))
  1286. pdata->chan_priority = tmp;
  1287. if (!of_property_read_u32(np, "block_size", &tmp))
  1288. pdata->block_size = tmp;
  1289. if (!of_property_read_u32(np, "dma-masters", &tmp)) {
  1290. if (tmp > 4)
  1291. return NULL;
  1292. pdata->nr_masters = tmp;
  1293. }
  1294. if (!of_property_read_u32_array(np, "data_width", arr,
  1295. pdata->nr_masters))
  1296. for (tmp = 0; tmp < pdata->nr_masters; tmp++)
  1297. pdata->data_width[tmp] = arr[tmp];
  1298. return pdata;
  1299. }
  1300. #else
  1301. static inline struct dw_dma_platform_data *
  1302. dw_dma_parse_dt(struct platform_device *pdev)
  1303. {
  1304. return NULL;
  1305. }
  1306. #endif
  1307. static int dw_probe(struct platform_device *pdev)
  1308. {
  1309. const struct platform_device_id *match;
  1310. struct dw_dma_platform_data *pdata;
  1311. struct resource *io;
  1312. struct dw_dma *dw;
  1313. size_t size;
  1314. void __iomem *regs;
  1315. bool autocfg;
  1316. unsigned int dw_params;
  1317. unsigned int nr_channels;
  1318. unsigned int max_blk_size = 0;
  1319. int irq;
  1320. int err;
  1321. int i;
  1322. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1323. if (!io)
  1324. return -EINVAL;
  1325. irq = platform_get_irq(pdev, 0);
  1326. if (irq < 0)
  1327. return irq;
  1328. regs = devm_ioremap_resource(&pdev->dev, io);
  1329. if (IS_ERR(regs))
  1330. return PTR_ERR(regs);
  1331. /* Apply default dma_mask if needed */
  1332. if (!pdev->dev.dma_mask) {
  1333. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  1334. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  1335. }
  1336. dw_params = dma_read_byaddr(regs, DW_PARAMS);
  1337. autocfg = dw_params >> DW_PARAMS_EN & 0x1;
  1338. dev_dbg(&pdev->dev, "DW_PARAMS: 0x%08x\n", dw_params);
  1339. pdata = dev_get_platdata(&pdev->dev);
  1340. if (!pdata)
  1341. pdata = dw_dma_parse_dt(pdev);
  1342. if (!pdata && autocfg) {
  1343. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1344. if (!pdata)
  1345. return -ENOMEM;
  1346. /* Fill platform data with the default values */
  1347. pdata->is_private = true;
  1348. pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
  1349. pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
  1350. } else if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  1351. return -EINVAL;
  1352. if (autocfg)
  1353. nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 0x7) + 1;
  1354. else
  1355. nr_channels = pdata->nr_channels;
  1356. size = sizeof(struct dw_dma) + nr_channels * sizeof(struct dw_dma_chan);
  1357. dw = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
  1358. if (!dw)
  1359. return -ENOMEM;
  1360. dw->clk = devm_clk_get(&pdev->dev, "hclk");
  1361. if (IS_ERR(dw->clk))
  1362. return PTR_ERR(dw->clk);
  1363. clk_prepare_enable(dw->clk);
  1364. dw->regs = regs;
  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. /* Get the base request line if set */
  1378. match = platform_get_device_id(pdev);
  1379. if (match)
  1380. dw->request_line_base = (unsigned int)match->driver_data;
  1381. /* Calculate all channel mask before DMA setup */
  1382. dw->all_chan_mask = (1 << nr_channels) - 1;
  1383. /* force dma off, just in case */
  1384. dw_dma_off(dw);
  1385. /* disable BLOCK interrupts as well */
  1386. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1387. err = devm_request_irq(&pdev->dev, irq, dw_dma_interrupt, 0,
  1388. "dw_dmac", dw);
  1389. if (err)
  1390. return err;
  1391. platform_set_drvdata(pdev, dw);
  1392. /* create a pool of consistent memory blocks for hardware descriptors */
  1393. dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", &pdev->dev,
  1394. sizeof(struct dw_desc), 4, 0);
  1395. if (!dw->desc_pool) {
  1396. dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
  1397. return -ENOMEM;
  1398. }
  1399. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1400. INIT_LIST_HEAD(&dw->dma.channels);
  1401. for (i = 0; i < nr_channels; i++) {
  1402. struct dw_dma_chan *dwc = &dw->chan[i];
  1403. int r = nr_channels - i - 1;
  1404. dwc->chan.device = &dw->dma;
  1405. dma_cookie_init(&dwc->chan);
  1406. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  1407. list_add_tail(&dwc->chan.device_node,
  1408. &dw->dma.channels);
  1409. else
  1410. list_add(&dwc->chan.device_node, &dw->dma.channels);
  1411. /* 7 is highest priority & 0 is lowest. */
  1412. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  1413. dwc->priority = r;
  1414. else
  1415. dwc->priority = i;
  1416. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1417. spin_lock_init(&dwc->lock);
  1418. dwc->mask = 1 << i;
  1419. INIT_LIST_HEAD(&dwc->active_list);
  1420. INIT_LIST_HEAD(&dwc->queue);
  1421. INIT_LIST_HEAD(&dwc->free_list);
  1422. channel_clear_bit(dw, CH_EN, dwc->mask);
  1423. dwc->direction = DMA_TRANS_NONE;
  1424. /* hardware configuration */
  1425. if (autocfg) {
  1426. unsigned int dwc_params;
  1427. dwc_params = dma_read_byaddr(regs + r * sizeof(u32),
  1428. DWC_PARAMS);
  1429. dev_dbg(&pdev->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
  1430. dwc_params);
  1431. /* Decode maximum block size for given channel. The
  1432. * stored 4 bit value represents blocks from 0x00 for 3
  1433. * up to 0x0a for 4095. */
  1434. dwc->block_size =
  1435. (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
  1436. dwc->nollp =
  1437. (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
  1438. } else {
  1439. dwc->block_size = pdata->block_size;
  1440. /* Check if channel supports multi block transfer */
  1441. channel_writel(dwc, LLP, 0xfffffffc);
  1442. dwc->nollp =
  1443. (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
  1444. channel_writel(dwc, LLP, 0);
  1445. }
  1446. }
  1447. /* Clear all interrupts on all channels. */
  1448. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1449. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1450. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1451. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1452. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1453. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1454. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1455. if (pdata->is_private)
  1456. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1457. dw->dma.dev = &pdev->dev;
  1458. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1459. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1460. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1461. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1462. dw->dma.device_control = dwc_control;
  1463. dw->dma.device_tx_status = dwc_tx_status;
  1464. dw->dma.device_issue_pending = dwc_issue_pending;
  1465. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1466. dev_info(&pdev->dev, "DesignWare DMA Controller, %d channels\n",
  1467. nr_channels);
  1468. dma_async_device_register(&dw->dma);
  1469. if (pdev->dev.of_node) {
  1470. err = of_dma_controller_register(pdev->dev.of_node,
  1471. dw_dma_xlate, dw);
  1472. if (err && err != -ENODEV)
  1473. dev_err(&pdev->dev,
  1474. "could not register of_dma_controller\n");
  1475. }
  1476. return 0;
  1477. }
  1478. static int dw_remove(struct platform_device *pdev)
  1479. {
  1480. struct dw_dma *dw = platform_get_drvdata(pdev);
  1481. struct dw_dma_chan *dwc, *_dwc;
  1482. if (pdev->dev.of_node)
  1483. of_dma_controller_free(pdev->dev.of_node);
  1484. dw_dma_off(dw);
  1485. dma_async_device_unregister(&dw->dma);
  1486. tasklet_kill(&dw->tasklet);
  1487. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1488. chan.device_node) {
  1489. list_del(&dwc->chan.device_node);
  1490. channel_clear_bit(dw, CH_EN, dwc->mask);
  1491. }
  1492. return 0;
  1493. }
  1494. static void dw_shutdown(struct platform_device *pdev)
  1495. {
  1496. struct dw_dma *dw = platform_get_drvdata(pdev);
  1497. dw_dma_off(dw);
  1498. clk_disable_unprepare(dw->clk);
  1499. }
  1500. static int dw_suspend_noirq(struct device *dev)
  1501. {
  1502. struct platform_device *pdev = to_platform_device(dev);
  1503. struct dw_dma *dw = platform_get_drvdata(pdev);
  1504. dw_dma_off(dw);
  1505. clk_disable_unprepare(dw->clk);
  1506. return 0;
  1507. }
  1508. static int dw_resume_noirq(struct device *dev)
  1509. {
  1510. struct platform_device *pdev = to_platform_device(dev);
  1511. struct dw_dma *dw = platform_get_drvdata(pdev);
  1512. clk_prepare_enable(dw->clk);
  1513. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1514. return 0;
  1515. }
  1516. static const struct dev_pm_ops dw_dev_pm_ops = {
  1517. .suspend_noirq = dw_suspend_noirq,
  1518. .resume_noirq = dw_resume_noirq,
  1519. .freeze_noirq = dw_suspend_noirq,
  1520. .thaw_noirq = dw_resume_noirq,
  1521. .restore_noirq = dw_resume_noirq,
  1522. .poweroff_noirq = dw_suspend_noirq,
  1523. };
  1524. #ifdef CONFIG_OF
  1525. static const struct of_device_id dw_dma_id_table[] = {
  1526. { .compatible = "snps,dma-spear1340" },
  1527. {}
  1528. };
  1529. MODULE_DEVICE_TABLE(of, dw_dma_id_table);
  1530. #endif
  1531. static const struct platform_device_id dw_dma_ids[] = {
  1532. /* Name, Request Line Base */
  1533. { "INTL9C60", (kernel_ulong_t)16 },
  1534. { }
  1535. };
  1536. static struct platform_driver dw_driver = {
  1537. .probe = dw_probe,
  1538. .remove = dw_remove,
  1539. .shutdown = dw_shutdown,
  1540. .driver = {
  1541. .name = "dw_dmac",
  1542. .pm = &dw_dev_pm_ops,
  1543. .of_match_table = of_match_ptr(dw_dma_id_table),
  1544. },
  1545. .id_table = dw_dma_ids,
  1546. };
  1547. static int __init dw_init(void)
  1548. {
  1549. return platform_driver_register(&dw_driver);
  1550. }
  1551. subsys_initcall(dw_init);
  1552. static void __exit dw_exit(void)
  1553. {
  1554. platform_driver_unregister(&dw_driver);
  1555. }
  1556. module_exit(dw_exit);
  1557. MODULE_LICENSE("GPL v2");
  1558. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  1559. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1560. MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");