dw_dmac.c 49 KB

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