dw_dmac.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. /*
  2. * Driver for the Synopsys DesignWare DMA Controller (aka DMACA on
  3. * AVR32 systems.)
  4. *
  5. * Copyright (C) 2007-2008 Atmel Corporation
  6. * Copyright (C) 2010-2011 ST Microelectronics
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/dmaengine.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include "dw_dmac_regs.h"
  24. /*
  25. * This supports the Synopsys "DesignWare AHB Central DMA Controller",
  26. * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
  27. * of which use ARM any more). See the "Databook" from Synopsys for
  28. * information beyond what licensees probably provide.
  29. *
  30. * The driver has currently been tested only with the Atmel AT32AP7000,
  31. * which does not support descriptor writeback.
  32. */
  33. #define DWC_DEFAULT_CTLLO(private) ({ \
  34. struct dw_dma_slave *__slave = (private); \
  35. int dms = __slave ? __slave->dst_master : 0; \
  36. int sms = __slave ? __slave->src_master : 1; \
  37. u8 smsize = __slave ? __slave->src_msize : DW_DMA_MSIZE_16; \
  38. u8 dmsize = __slave ? __slave->dst_msize : DW_DMA_MSIZE_16; \
  39. \
  40. (DWC_CTLL_DST_MSIZE(dmsize) \
  41. | DWC_CTLL_SRC_MSIZE(smsize) \
  42. | DWC_CTLL_LLP_D_EN \
  43. | DWC_CTLL_LLP_S_EN \
  44. | DWC_CTLL_DMS(dms) \
  45. | DWC_CTLL_SMS(sms)); \
  46. })
  47. /*
  48. * This is configuration-dependent and usually a funny size like 4095.
  49. *
  50. * Note that this is a transfer count, i.e. if we transfer 32-bit
  51. * words, we can do 16380 bytes per descriptor.
  52. *
  53. * This parameter is also system-specific.
  54. */
  55. #define DWC_MAX_COUNT 4095U
  56. /*
  57. * Number of descriptors to allocate for each channel. This should be
  58. * made configurable somehow; preferably, the clients (at least the
  59. * ones using slave transfers) should be able to give us a hint.
  60. */
  61. #define NR_DESCS_PER_CHANNEL 64
  62. /*----------------------------------------------------------------------*/
  63. /*
  64. * Because we're not relying on writeback from the controller (it may not
  65. * even be configured into the core!) we don't need to use dma_pool. These
  66. * descriptors -- and associated data -- are cacheable. We do need to make
  67. * sure their dcache entries are written back before handing them off to
  68. * the controller, though.
  69. */
  70. static struct device *chan2dev(struct dma_chan *chan)
  71. {
  72. return &chan->dev->device;
  73. }
  74. static struct device *chan2parent(struct dma_chan *chan)
  75. {
  76. return chan->dev->device.parent;
  77. }
  78. static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
  79. {
  80. return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
  81. }
  82. static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
  83. {
  84. struct dw_desc *desc, *_desc;
  85. struct dw_desc *ret = NULL;
  86. unsigned int i = 0;
  87. unsigned long flags;
  88. spin_lock_irqsave(&dwc->lock, flags);
  89. list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
  90. if (async_tx_test_ack(&desc->txd)) {
  91. list_del(&desc->desc_node);
  92. ret = desc;
  93. break;
  94. }
  95. dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
  96. i++;
  97. }
  98. spin_unlock_irqrestore(&dwc->lock, flags);
  99. dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
  100. return ret;
  101. }
  102. static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
  103. {
  104. struct dw_desc *child;
  105. list_for_each_entry(child, &desc->tx_list, desc_node)
  106. dma_sync_single_for_cpu(chan2parent(&dwc->chan),
  107. child->txd.phys, sizeof(child->lli),
  108. DMA_TO_DEVICE);
  109. dma_sync_single_for_cpu(chan2parent(&dwc->chan),
  110. desc->txd.phys, sizeof(desc->lli),
  111. DMA_TO_DEVICE);
  112. }
  113. /*
  114. * Move a descriptor, including any children, to the free list.
  115. * `desc' must not be on any lists.
  116. */
  117. static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
  118. {
  119. unsigned long flags;
  120. if (desc) {
  121. struct dw_desc *child;
  122. dwc_sync_desc_for_cpu(dwc, desc);
  123. spin_lock_irqsave(&dwc->lock, flags);
  124. list_for_each_entry(child, &desc->tx_list, desc_node)
  125. dev_vdbg(chan2dev(&dwc->chan),
  126. "moving child desc %p to freelist\n",
  127. child);
  128. list_splice_init(&desc->tx_list, &dwc->free_list);
  129. dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
  130. list_add(&desc->desc_node, &dwc->free_list);
  131. spin_unlock_irqrestore(&dwc->lock, flags);
  132. }
  133. }
  134. /* Called with dwc->lock held and bh disabled */
  135. static dma_cookie_t
  136. dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
  137. {
  138. dma_cookie_t cookie = dwc->chan.cookie;
  139. if (++cookie < 0)
  140. cookie = 1;
  141. dwc->chan.cookie = cookie;
  142. desc->txd.cookie = cookie;
  143. return cookie;
  144. }
  145. /*----------------------------------------------------------------------*/
  146. /* Called with dwc->lock held and bh disabled */
  147. static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
  148. {
  149. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  150. /* ASSERT: channel is idle */
  151. if (dma_readl(dw, CH_EN) & dwc->mask) {
  152. dev_err(chan2dev(&dwc->chan),
  153. "BUG: Attempted to start non-idle channel\n");
  154. dev_err(chan2dev(&dwc->chan),
  155. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  156. channel_readl(dwc, SAR),
  157. channel_readl(dwc, DAR),
  158. channel_readl(dwc, LLP),
  159. channel_readl(dwc, CTL_HI),
  160. channel_readl(dwc, CTL_LO));
  161. /* The tasklet will hopefully advance the queue... */
  162. return;
  163. }
  164. channel_writel(dwc, LLP, first->txd.phys);
  165. channel_writel(dwc, CTL_LO,
  166. DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  167. channel_writel(dwc, CTL_HI, 0);
  168. channel_set_bit(dw, CH_EN, dwc->mask);
  169. }
  170. /*----------------------------------------------------------------------*/
  171. static void
  172. dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
  173. bool callback_required)
  174. {
  175. dma_async_tx_callback callback = NULL;
  176. void *param = NULL;
  177. struct dma_async_tx_descriptor *txd = &desc->txd;
  178. struct dw_desc *child;
  179. unsigned long flags;
  180. dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
  181. spin_lock_irqsave(&dwc->lock, flags);
  182. dwc->completed = txd->cookie;
  183. if (callback_required) {
  184. callback = txd->callback;
  185. param = txd->callback_param;
  186. }
  187. dwc_sync_desc_for_cpu(dwc, desc);
  188. /* async_tx_ack */
  189. list_for_each_entry(child, &desc->tx_list, desc_node)
  190. async_tx_ack(&child->txd);
  191. async_tx_ack(&desc->txd);
  192. list_splice_init(&desc->tx_list, &dwc->free_list);
  193. list_move(&desc->desc_node, &dwc->free_list);
  194. if (!dwc->chan.private) {
  195. struct device *parent = chan2parent(&dwc->chan);
  196. if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  197. if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  198. dma_unmap_single(parent, desc->lli.dar,
  199. desc->len, DMA_FROM_DEVICE);
  200. else
  201. dma_unmap_page(parent, desc->lli.dar,
  202. desc->len, DMA_FROM_DEVICE);
  203. }
  204. if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  205. if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  206. dma_unmap_single(parent, desc->lli.sar,
  207. desc->len, DMA_TO_DEVICE);
  208. else
  209. dma_unmap_page(parent, desc->lli.sar,
  210. desc->len, DMA_TO_DEVICE);
  211. }
  212. }
  213. spin_unlock_irqrestore(&dwc->lock, flags);
  214. if (callback_required && callback)
  215. callback(param);
  216. }
  217. static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
  218. {
  219. struct dw_desc *desc, *_desc;
  220. LIST_HEAD(list);
  221. unsigned long flags;
  222. spin_lock_irqsave(&dwc->lock, flags);
  223. if (dma_readl(dw, CH_EN) & dwc->mask) {
  224. dev_err(chan2dev(&dwc->chan),
  225. "BUG: XFER bit set, but channel not idle!\n");
  226. /* Try to continue after resetting the channel... */
  227. channel_clear_bit(dw, CH_EN, dwc->mask);
  228. while (dma_readl(dw, CH_EN) & dwc->mask)
  229. cpu_relax();
  230. }
  231. /*
  232. * Submit queued descriptors ASAP, i.e. before we go through
  233. * the completed ones.
  234. */
  235. list_splice_init(&dwc->active_list, &list);
  236. if (!list_empty(&dwc->queue)) {
  237. list_move(dwc->queue.next, &dwc->active_list);
  238. dwc_dostart(dwc, dwc_first_active(dwc));
  239. }
  240. spin_unlock_irqrestore(&dwc->lock, flags);
  241. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  242. dwc_descriptor_complete(dwc, desc, true);
  243. }
  244. static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
  245. {
  246. dma_addr_t llp;
  247. struct dw_desc *desc, *_desc;
  248. struct dw_desc *child;
  249. u32 status_xfer;
  250. unsigned long flags;
  251. spin_lock_irqsave(&dwc->lock, flags);
  252. /*
  253. * Clear block interrupt flag before scanning so that we don't
  254. * miss any, and read LLP before RAW_XFER to ensure it is
  255. * valid if we decide to scan the list.
  256. */
  257. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  258. llp = channel_readl(dwc, LLP);
  259. status_xfer = dma_readl(dw, RAW.XFER);
  260. if (status_xfer & dwc->mask) {
  261. /* Everything we've submitted is done */
  262. dma_writel(dw, CLEAR.XFER, dwc->mask);
  263. spin_unlock_irqrestore(&dwc->lock, flags);
  264. dwc_complete_all(dw, dwc);
  265. return;
  266. }
  267. if (list_empty(&dwc->active_list)) {
  268. spin_unlock_irqrestore(&dwc->lock, flags);
  269. return;
  270. }
  271. dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
  272. list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
  273. /* check first descriptors addr */
  274. if (desc->txd.phys == llp) {
  275. spin_unlock_irqrestore(&dwc->lock, flags);
  276. return;
  277. }
  278. /* check first descriptors llp */
  279. if (desc->lli.llp == llp) {
  280. /* This one is currently in progress */
  281. spin_unlock_irqrestore(&dwc->lock, flags);
  282. return;
  283. }
  284. list_for_each_entry(child, &desc->tx_list, desc_node)
  285. if (child->lli.llp == llp) {
  286. /* Currently in progress */
  287. spin_unlock_irqrestore(&dwc->lock, flags);
  288. return;
  289. }
  290. /*
  291. * No descriptors so far seem to be in progress, i.e.
  292. * this one must be done.
  293. */
  294. spin_unlock_irqrestore(&dwc->lock, flags);
  295. dwc_descriptor_complete(dwc, desc, true);
  296. spin_lock_irqsave(&dwc->lock, flags);
  297. }
  298. dev_err(chan2dev(&dwc->chan),
  299. "BUG: All descriptors done, but channel not idle!\n");
  300. /* Try to continue after resetting the channel... */
  301. channel_clear_bit(dw, CH_EN, dwc->mask);
  302. while (dma_readl(dw, CH_EN) & dwc->mask)
  303. cpu_relax();
  304. if (!list_empty(&dwc->queue)) {
  305. list_move(dwc->queue.next, &dwc->active_list);
  306. dwc_dostart(dwc, dwc_first_active(dwc));
  307. }
  308. spin_unlock_irqrestore(&dwc->lock, flags);
  309. }
  310. static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
  311. {
  312. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  313. " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
  314. lli->sar, lli->dar, lli->llp,
  315. lli->ctlhi, lli->ctllo);
  316. }
  317. static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
  318. {
  319. struct dw_desc *bad_desc;
  320. struct dw_desc *child;
  321. unsigned long flags;
  322. dwc_scan_descriptors(dw, dwc);
  323. spin_lock_irqsave(&dwc->lock, flags);
  324. /*
  325. * The descriptor currently at the head of the active list is
  326. * borked. Since we don't have any way to report errors, we'll
  327. * just have to scream loudly and try to carry on.
  328. */
  329. bad_desc = dwc_first_active(dwc);
  330. list_del_init(&bad_desc->desc_node);
  331. list_move(dwc->queue.next, dwc->active_list.prev);
  332. /* Clear the error flag and try to restart the controller */
  333. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  334. if (!list_empty(&dwc->active_list))
  335. dwc_dostart(dwc, dwc_first_active(dwc));
  336. /*
  337. * KERN_CRITICAL may seem harsh, but since this only happens
  338. * when someone submits a bad physical address in a
  339. * descriptor, we should consider ourselves lucky that the
  340. * controller flagged an error instead of scribbling over
  341. * random memory locations.
  342. */
  343. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  344. "Bad descriptor submitted for DMA!\n");
  345. dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
  346. " cookie: %d\n", bad_desc->txd.cookie);
  347. dwc_dump_lli(dwc, &bad_desc->lli);
  348. list_for_each_entry(child, &bad_desc->tx_list, desc_node)
  349. dwc_dump_lli(dwc, &child->lli);
  350. spin_unlock_irqrestore(&dwc->lock, flags);
  351. /* Pretend the descriptor completed successfully */
  352. dwc_descriptor_complete(dwc, bad_desc, true);
  353. }
  354. /* --------------------- Cyclic DMA API extensions -------------------- */
  355. inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
  356. {
  357. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  358. return channel_readl(dwc, SAR);
  359. }
  360. EXPORT_SYMBOL(dw_dma_get_src_addr);
  361. inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
  362. {
  363. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  364. return channel_readl(dwc, DAR);
  365. }
  366. EXPORT_SYMBOL(dw_dma_get_dst_addr);
  367. /* called with dwc->lock held and all DMAC interrupts disabled */
  368. static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
  369. u32 status_block, u32 status_err, u32 status_xfer)
  370. {
  371. unsigned long flags;
  372. if (status_block & dwc->mask) {
  373. void (*callback)(void *param);
  374. void *callback_param;
  375. dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
  376. channel_readl(dwc, LLP));
  377. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  378. callback = dwc->cdesc->period_callback;
  379. callback_param = dwc->cdesc->period_callback_param;
  380. if (callback)
  381. callback(callback_param);
  382. }
  383. /*
  384. * Error and transfer complete are highly unlikely, and will most
  385. * likely be due to a configuration error by the user.
  386. */
  387. if (unlikely(status_err & dwc->mask) ||
  388. unlikely(status_xfer & dwc->mask)) {
  389. int i;
  390. dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
  391. "interrupt, stopping DMA transfer\n",
  392. status_xfer ? "xfer" : "error");
  393. spin_lock_irqsave(&dwc->lock, flags);
  394. dev_err(chan2dev(&dwc->chan),
  395. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  396. channel_readl(dwc, SAR),
  397. channel_readl(dwc, DAR),
  398. channel_readl(dwc, LLP),
  399. channel_readl(dwc, CTL_HI),
  400. channel_readl(dwc, CTL_LO));
  401. channel_clear_bit(dw, CH_EN, dwc->mask);
  402. while (dma_readl(dw, CH_EN) & dwc->mask)
  403. cpu_relax();
  404. /* make sure DMA does not restart by loading a new list */
  405. channel_writel(dwc, LLP, 0);
  406. channel_writel(dwc, CTL_LO, 0);
  407. channel_writel(dwc, CTL_HI, 0);
  408. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  409. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  410. dma_writel(dw, CLEAR.XFER, dwc->mask);
  411. for (i = 0; i < dwc->cdesc->periods; i++)
  412. dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
  413. spin_unlock_irqrestore(&dwc->lock, flags);
  414. }
  415. }
  416. /* ------------------------------------------------------------------------- */
  417. static void dw_dma_tasklet(unsigned long data)
  418. {
  419. struct dw_dma *dw = (struct dw_dma *)data;
  420. struct dw_dma_chan *dwc;
  421. u32 status_block;
  422. u32 status_xfer;
  423. u32 status_err;
  424. int i;
  425. status_block = dma_readl(dw, RAW.BLOCK);
  426. status_xfer = dma_readl(dw, RAW.XFER);
  427. status_err = dma_readl(dw, RAW.ERROR);
  428. dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
  429. status_block, status_err);
  430. for (i = 0; i < dw->dma.chancnt; i++) {
  431. dwc = &dw->chan[i];
  432. if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
  433. dwc_handle_cyclic(dw, dwc, status_block, status_err,
  434. status_xfer);
  435. else if (status_err & (1 << i))
  436. dwc_handle_error(dw, dwc);
  437. else if ((status_block | status_xfer) & (1 << i))
  438. dwc_scan_descriptors(dw, dwc);
  439. }
  440. /*
  441. * Re-enable interrupts. Block Complete interrupts are only
  442. * enabled if the INT_EN bit in the descriptor is set. This
  443. * will trigger a scan before the whole list is done.
  444. */
  445. channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
  446. channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  447. channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
  448. }
  449. static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
  450. {
  451. struct dw_dma *dw = dev_id;
  452. u32 status;
  453. dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
  454. dma_readl(dw, STATUS_INT));
  455. /*
  456. * Just disable the interrupts. We'll turn them back on in the
  457. * softirq handler.
  458. */
  459. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  460. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  461. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  462. status = dma_readl(dw, STATUS_INT);
  463. if (status) {
  464. dev_err(dw->dma.dev,
  465. "BUG: Unexpected interrupts pending: 0x%x\n",
  466. status);
  467. /* Try to recover */
  468. channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
  469. channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
  470. channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
  471. channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
  472. channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
  473. }
  474. tasklet_schedule(&dw->tasklet);
  475. return IRQ_HANDLED;
  476. }
  477. /*----------------------------------------------------------------------*/
  478. static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
  479. {
  480. struct dw_desc *desc = txd_to_dw_desc(tx);
  481. struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
  482. dma_cookie_t cookie;
  483. unsigned long flags;
  484. spin_lock_irqsave(&dwc->lock, flags);
  485. cookie = dwc_assign_cookie(dwc, desc);
  486. /*
  487. * REVISIT: We should attempt to chain as many descriptors as
  488. * possible, perhaps even appending to those already submitted
  489. * for DMA. But this is hard to do in a race-free manner.
  490. */
  491. if (list_empty(&dwc->active_list)) {
  492. dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
  493. desc->txd.cookie);
  494. list_add_tail(&desc->desc_node, &dwc->active_list);
  495. dwc_dostart(dwc, dwc_first_active(dwc));
  496. } else {
  497. dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
  498. desc->txd.cookie);
  499. list_add_tail(&desc->desc_node, &dwc->queue);
  500. }
  501. spin_unlock_irqrestore(&dwc->lock, flags);
  502. return cookie;
  503. }
  504. static struct dma_async_tx_descriptor *
  505. dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
  506. size_t len, unsigned long flags)
  507. {
  508. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  509. struct dw_desc *desc;
  510. struct dw_desc *first;
  511. struct dw_desc *prev;
  512. size_t xfer_count;
  513. size_t offset;
  514. unsigned int src_width;
  515. unsigned int dst_width;
  516. u32 ctllo;
  517. dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
  518. dest, src, len, flags);
  519. if (unlikely(!len)) {
  520. dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
  521. return NULL;
  522. }
  523. /*
  524. * We can be a lot more clever here, but this should take care
  525. * of the most common optimization.
  526. */
  527. if (!((src | dest | len) & 7))
  528. src_width = dst_width = 3;
  529. else if (!((src | dest | len) & 3))
  530. src_width = dst_width = 2;
  531. else if (!((src | dest | len) & 1))
  532. src_width = dst_width = 1;
  533. else
  534. src_width = dst_width = 0;
  535. ctllo = DWC_DEFAULT_CTLLO(chan->private)
  536. | DWC_CTLL_DST_WIDTH(dst_width)
  537. | DWC_CTLL_SRC_WIDTH(src_width)
  538. | DWC_CTLL_DST_INC
  539. | DWC_CTLL_SRC_INC
  540. | DWC_CTLL_FC_M2M;
  541. prev = first = NULL;
  542. for (offset = 0; offset < len; offset += xfer_count << src_width) {
  543. xfer_count = min_t(size_t, (len - offset) >> src_width,
  544. DWC_MAX_COUNT);
  545. desc = dwc_desc_get(dwc);
  546. if (!desc)
  547. goto err_desc_get;
  548. desc->lli.sar = src + offset;
  549. desc->lli.dar = dest + offset;
  550. desc->lli.ctllo = ctllo;
  551. desc->lli.ctlhi = xfer_count;
  552. if (!first) {
  553. first = desc;
  554. } else {
  555. prev->lli.llp = desc->txd.phys;
  556. dma_sync_single_for_device(chan2parent(chan),
  557. prev->txd.phys, sizeof(prev->lli),
  558. DMA_TO_DEVICE);
  559. list_add_tail(&desc->desc_node,
  560. &first->tx_list);
  561. }
  562. prev = desc;
  563. }
  564. if (flags & DMA_PREP_INTERRUPT)
  565. /* Trigger interrupt after last block */
  566. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  567. prev->lli.llp = 0;
  568. dma_sync_single_for_device(chan2parent(chan),
  569. prev->txd.phys, sizeof(prev->lli),
  570. DMA_TO_DEVICE);
  571. first->txd.flags = flags;
  572. first->len = len;
  573. return &first->txd;
  574. err_desc_get:
  575. dwc_desc_put(dwc, first);
  576. return NULL;
  577. }
  578. static struct dma_async_tx_descriptor *
  579. dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
  580. unsigned int sg_len, enum dma_data_direction direction,
  581. unsigned long flags)
  582. {
  583. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  584. struct dw_dma_slave *dws = chan->private;
  585. struct dw_desc *prev;
  586. struct dw_desc *first;
  587. u32 ctllo;
  588. dma_addr_t reg;
  589. unsigned int reg_width;
  590. unsigned int mem_width;
  591. unsigned int i;
  592. struct scatterlist *sg;
  593. size_t total_len = 0;
  594. dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
  595. if (unlikely(!dws || !sg_len))
  596. return NULL;
  597. reg_width = dws->reg_width;
  598. prev = first = NULL;
  599. switch (direction) {
  600. case DMA_TO_DEVICE:
  601. ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  602. | DWC_CTLL_DST_WIDTH(reg_width)
  603. | DWC_CTLL_DST_FIX
  604. | DWC_CTLL_SRC_INC
  605. | DWC_CTLL_FC(dws->fc));
  606. reg = dws->tx_reg;
  607. for_each_sg(sgl, sg, sg_len, i) {
  608. struct dw_desc *desc;
  609. u32 len, dlen, mem;
  610. mem = sg_phys(sg);
  611. len = sg_dma_len(sg);
  612. mem_width = 2;
  613. if (unlikely(mem & 3 || len & 3))
  614. mem_width = 0;
  615. slave_sg_todev_fill_desc:
  616. desc = dwc_desc_get(dwc);
  617. if (!desc) {
  618. dev_err(chan2dev(chan),
  619. "not enough descriptors available\n");
  620. goto err_desc_get;
  621. }
  622. desc->lli.sar = mem;
  623. desc->lli.dar = reg;
  624. desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
  625. if ((len >> mem_width) > DWC_MAX_COUNT) {
  626. dlen = DWC_MAX_COUNT << mem_width;
  627. mem += dlen;
  628. len -= dlen;
  629. } else {
  630. dlen = len;
  631. len = 0;
  632. }
  633. desc->lli.ctlhi = dlen >> mem_width;
  634. if (!first) {
  635. first = desc;
  636. } else {
  637. prev->lli.llp = desc->txd.phys;
  638. dma_sync_single_for_device(chan2parent(chan),
  639. prev->txd.phys,
  640. sizeof(prev->lli),
  641. DMA_TO_DEVICE);
  642. list_add_tail(&desc->desc_node,
  643. &first->tx_list);
  644. }
  645. prev = desc;
  646. total_len += dlen;
  647. if (len)
  648. goto slave_sg_todev_fill_desc;
  649. }
  650. break;
  651. case DMA_FROM_DEVICE:
  652. ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  653. | DWC_CTLL_SRC_WIDTH(reg_width)
  654. | DWC_CTLL_DST_INC
  655. | DWC_CTLL_SRC_FIX
  656. | DWC_CTLL_FC(dws->fc));
  657. reg = dws->rx_reg;
  658. for_each_sg(sgl, sg, sg_len, i) {
  659. struct dw_desc *desc;
  660. u32 len, dlen, mem;
  661. mem = sg_phys(sg);
  662. len = sg_dma_len(sg);
  663. mem_width = 2;
  664. if (unlikely(mem & 3 || len & 3))
  665. mem_width = 0;
  666. slave_sg_fromdev_fill_desc:
  667. desc = dwc_desc_get(dwc);
  668. if (!desc) {
  669. dev_err(chan2dev(chan),
  670. "not enough descriptors available\n");
  671. goto err_desc_get;
  672. }
  673. desc->lli.sar = reg;
  674. desc->lli.dar = mem;
  675. desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
  676. if ((len >> reg_width) > DWC_MAX_COUNT) {
  677. dlen = DWC_MAX_COUNT << reg_width;
  678. mem += dlen;
  679. len -= dlen;
  680. } else {
  681. dlen = len;
  682. len = 0;
  683. }
  684. desc->lli.ctlhi = dlen >> reg_width;
  685. if (!first) {
  686. first = desc;
  687. } else {
  688. prev->lli.llp = desc->txd.phys;
  689. dma_sync_single_for_device(chan2parent(chan),
  690. prev->txd.phys,
  691. sizeof(prev->lli),
  692. DMA_TO_DEVICE);
  693. list_add_tail(&desc->desc_node,
  694. &first->tx_list);
  695. }
  696. prev = desc;
  697. total_len += dlen;
  698. if (len)
  699. goto slave_sg_fromdev_fill_desc;
  700. }
  701. break;
  702. default:
  703. return NULL;
  704. }
  705. if (flags & DMA_PREP_INTERRUPT)
  706. /* Trigger interrupt after last block */
  707. prev->lli.ctllo |= DWC_CTLL_INT_EN;
  708. prev->lli.llp = 0;
  709. dma_sync_single_for_device(chan2parent(chan),
  710. prev->txd.phys, sizeof(prev->lli),
  711. DMA_TO_DEVICE);
  712. first->len = total_len;
  713. return &first->txd;
  714. err_desc_get:
  715. dwc_desc_put(dwc, first);
  716. return NULL;
  717. }
  718. static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
  719. unsigned long arg)
  720. {
  721. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  722. struct dw_dma *dw = to_dw_dma(chan->device);
  723. struct dw_desc *desc, *_desc;
  724. unsigned long flags;
  725. u32 cfglo;
  726. LIST_HEAD(list);
  727. if (cmd == DMA_PAUSE) {
  728. spin_lock_irqsave(&dwc->lock, flags);
  729. cfglo = channel_readl(dwc, CFG_LO);
  730. channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
  731. while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY))
  732. cpu_relax();
  733. dwc->paused = true;
  734. spin_unlock_irqrestore(&dwc->lock, flags);
  735. } else if (cmd == DMA_RESUME) {
  736. if (!dwc->paused)
  737. return 0;
  738. spin_lock_irqsave(&dwc->lock, flags);
  739. cfglo = channel_readl(dwc, CFG_LO);
  740. channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
  741. dwc->paused = false;
  742. spin_unlock_irqrestore(&dwc->lock, flags);
  743. } else if (cmd == DMA_TERMINATE_ALL) {
  744. spin_lock_irqsave(&dwc->lock, flags);
  745. channel_clear_bit(dw, CH_EN, dwc->mask);
  746. while (dma_readl(dw, CH_EN) & dwc->mask)
  747. cpu_relax();
  748. dwc->paused = false;
  749. /* active_list entries will end up before queued entries */
  750. list_splice_init(&dwc->queue, &list);
  751. list_splice_init(&dwc->active_list, &list);
  752. spin_unlock_irqrestore(&dwc->lock, flags);
  753. /* Flush all pending and queued descriptors */
  754. list_for_each_entry_safe(desc, _desc, &list, desc_node)
  755. dwc_descriptor_complete(dwc, desc, false);
  756. } else
  757. return -ENXIO;
  758. return 0;
  759. }
  760. static enum dma_status
  761. dwc_tx_status(struct dma_chan *chan,
  762. dma_cookie_t cookie,
  763. struct dma_tx_state *txstate)
  764. {
  765. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  766. dma_cookie_t last_used;
  767. dma_cookie_t last_complete;
  768. int ret;
  769. last_complete = dwc->completed;
  770. last_used = chan->cookie;
  771. ret = dma_async_is_complete(cookie, last_complete, last_used);
  772. if (ret != DMA_SUCCESS) {
  773. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  774. last_complete = dwc->completed;
  775. last_used = chan->cookie;
  776. ret = dma_async_is_complete(cookie, last_complete, last_used);
  777. }
  778. if (ret != DMA_SUCCESS)
  779. dma_set_tx_state(txstate, last_complete, last_used,
  780. dwc_first_active(dwc)->len);
  781. else
  782. dma_set_tx_state(txstate, last_complete, last_used, 0);
  783. if (dwc->paused)
  784. return DMA_PAUSED;
  785. return ret;
  786. }
  787. static void dwc_issue_pending(struct dma_chan *chan)
  788. {
  789. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  790. if (!list_empty(&dwc->queue))
  791. dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
  792. }
  793. static int dwc_alloc_chan_resources(struct dma_chan *chan)
  794. {
  795. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  796. struct dw_dma *dw = to_dw_dma(chan->device);
  797. struct dw_desc *desc;
  798. struct dw_dma_slave *dws;
  799. int i;
  800. u32 cfghi;
  801. u32 cfglo;
  802. unsigned long flags;
  803. dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
  804. /* ASSERT: channel is idle */
  805. if (dma_readl(dw, CH_EN) & dwc->mask) {
  806. dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
  807. return -EIO;
  808. }
  809. dwc->completed = chan->cookie = 1;
  810. cfghi = DWC_CFGH_FIFO_MODE;
  811. cfglo = 0;
  812. dws = chan->private;
  813. if (dws) {
  814. /*
  815. * We need controller-specific data to set up slave
  816. * transfers.
  817. */
  818. BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
  819. cfghi = dws->cfg_hi;
  820. cfglo = dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK;
  821. }
  822. cfglo |= DWC_CFGL_CH_PRIOR(dwc->priority);
  823. channel_writel(dwc, CFG_LO, cfglo);
  824. channel_writel(dwc, CFG_HI, cfghi);
  825. /*
  826. * NOTE: some controllers may have additional features that we
  827. * need to initialize here, like "scatter-gather" (which
  828. * doesn't mean what you think it means), and status writeback.
  829. */
  830. spin_lock_irqsave(&dwc->lock, flags);
  831. i = dwc->descs_allocated;
  832. while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
  833. spin_unlock_irqrestore(&dwc->lock, flags);
  834. desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
  835. if (!desc) {
  836. dev_info(chan2dev(chan),
  837. "only allocated %d descriptors\n", i);
  838. spin_lock_irqsave(&dwc->lock, flags);
  839. break;
  840. }
  841. INIT_LIST_HEAD(&desc->tx_list);
  842. dma_async_tx_descriptor_init(&desc->txd, chan);
  843. desc->txd.tx_submit = dwc_tx_submit;
  844. desc->txd.flags = DMA_CTRL_ACK;
  845. desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
  846. sizeof(desc->lli), DMA_TO_DEVICE);
  847. dwc_desc_put(dwc, desc);
  848. spin_lock_irqsave(&dwc->lock, flags);
  849. i = ++dwc->descs_allocated;
  850. }
  851. /* Enable interrupts */
  852. channel_set_bit(dw, MASK.XFER, dwc->mask);
  853. channel_set_bit(dw, MASK.BLOCK, dwc->mask);
  854. channel_set_bit(dw, MASK.ERROR, dwc->mask);
  855. spin_unlock_irqrestore(&dwc->lock, flags);
  856. dev_dbg(chan2dev(chan),
  857. "alloc_chan_resources allocated %d descriptors\n", i);
  858. return i;
  859. }
  860. static void dwc_free_chan_resources(struct dma_chan *chan)
  861. {
  862. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  863. struct dw_dma *dw = to_dw_dma(chan->device);
  864. struct dw_desc *desc, *_desc;
  865. unsigned long flags;
  866. LIST_HEAD(list);
  867. dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
  868. dwc->descs_allocated);
  869. /* ASSERT: channel is idle */
  870. BUG_ON(!list_empty(&dwc->active_list));
  871. BUG_ON(!list_empty(&dwc->queue));
  872. BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
  873. spin_lock_irqsave(&dwc->lock, flags);
  874. list_splice_init(&dwc->free_list, &list);
  875. dwc->descs_allocated = 0;
  876. /* Disable interrupts */
  877. channel_clear_bit(dw, MASK.XFER, dwc->mask);
  878. channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
  879. channel_clear_bit(dw, MASK.ERROR, dwc->mask);
  880. spin_unlock_irqrestore(&dwc->lock, flags);
  881. list_for_each_entry_safe(desc, _desc, &list, desc_node) {
  882. dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
  883. dma_unmap_single(chan2parent(chan), desc->txd.phys,
  884. sizeof(desc->lli), DMA_TO_DEVICE);
  885. kfree(desc);
  886. }
  887. dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
  888. }
  889. /* --------------------- Cyclic DMA API extensions -------------------- */
  890. /**
  891. * dw_dma_cyclic_start - start the cyclic DMA transfer
  892. * @chan: the DMA channel to start
  893. *
  894. * Must be called with soft interrupts disabled. Returns zero on success or
  895. * -errno on failure.
  896. */
  897. int dw_dma_cyclic_start(struct dma_chan *chan)
  898. {
  899. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  900. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  901. unsigned long flags;
  902. if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
  903. dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
  904. return -ENODEV;
  905. }
  906. spin_lock_irqsave(&dwc->lock, flags);
  907. /* assert channel is idle */
  908. if (dma_readl(dw, CH_EN) & dwc->mask) {
  909. dev_err(chan2dev(&dwc->chan),
  910. "BUG: Attempted to start non-idle channel\n");
  911. dev_err(chan2dev(&dwc->chan),
  912. " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
  913. channel_readl(dwc, SAR),
  914. channel_readl(dwc, DAR),
  915. channel_readl(dwc, LLP),
  916. channel_readl(dwc, CTL_HI),
  917. channel_readl(dwc, CTL_LO));
  918. spin_unlock_irqrestore(&dwc->lock, flags);
  919. return -EBUSY;
  920. }
  921. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  922. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  923. dma_writel(dw, CLEAR.XFER, dwc->mask);
  924. /* setup DMAC channel registers */
  925. channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
  926. channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
  927. channel_writel(dwc, CTL_HI, 0);
  928. channel_set_bit(dw, CH_EN, dwc->mask);
  929. spin_unlock_irqrestore(&dwc->lock, flags);
  930. return 0;
  931. }
  932. EXPORT_SYMBOL(dw_dma_cyclic_start);
  933. /**
  934. * dw_dma_cyclic_stop - stop the cyclic DMA transfer
  935. * @chan: the DMA channel to stop
  936. *
  937. * Must be called with soft interrupts disabled.
  938. */
  939. void dw_dma_cyclic_stop(struct dma_chan *chan)
  940. {
  941. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  942. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  943. unsigned long flags;
  944. spin_lock_irqsave(&dwc->lock, flags);
  945. channel_clear_bit(dw, CH_EN, dwc->mask);
  946. while (dma_readl(dw, CH_EN) & dwc->mask)
  947. cpu_relax();
  948. spin_unlock_irqrestore(&dwc->lock, flags);
  949. }
  950. EXPORT_SYMBOL(dw_dma_cyclic_stop);
  951. /**
  952. * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
  953. * @chan: the DMA channel to prepare
  954. * @buf_addr: physical DMA address where the buffer starts
  955. * @buf_len: total number of bytes for the entire buffer
  956. * @period_len: number of bytes for each period
  957. * @direction: transfer direction, to or from device
  958. *
  959. * Must be called before trying to start the transfer. Returns a valid struct
  960. * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
  961. */
  962. struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
  963. dma_addr_t buf_addr, size_t buf_len, size_t period_len,
  964. enum dma_data_direction direction)
  965. {
  966. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  967. struct dw_cyclic_desc *cdesc;
  968. struct dw_cyclic_desc *retval = NULL;
  969. struct dw_desc *desc;
  970. struct dw_desc *last = NULL;
  971. struct dw_dma_slave *dws = chan->private;
  972. unsigned long was_cyclic;
  973. unsigned int reg_width;
  974. unsigned int periods;
  975. unsigned int i;
  976. unsigned long flags;
  977. spin_lock_irqsave(&dwc->lock, flags);
  978. if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
  979. spin_unlock_irqrestore(&dwc->lock, flags);
  980. dev_dbg(chan2dev(&dwc->chan),
  981. "queue and/or active list are not empty\n");
  982. return ERR_PTR(-EBUSY);
  983. }
  984. was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  985. spin_unlock_irqrestore(&dwc->lock, flags);
  986. if (was_cyclic) {
  987. dev_dbg(chan2dev(&dwc->chan),
  988. "channel already prepared for cyclic DMA\n");
  989. return ERR_PTR(-EBUSY);
  990. }
  991. retval = ERR_PTR(-EINVAL);
  992. reg_width = dws->reg_width;
  993. periods = buf_len / period_len;
  994. /* Check for too big/unaligned periods and unaligned DMA buffer. */
  995. if (period_len > (DWC_MAX_COUNT << reg_width))
  996. goto out_err;
  997. if (unlikely(period_len & ((1 << reg_width) - 1)))
  998. goto out_err;
  999. if (unlikely(buf_addr & ((1 << reg_width) - 1)))
  1000. goto out_err;
  1001. if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
  1002. goto out_err;
  1003. retval = ERR_PTR(-ENOMEM);
  1004. if (periods > NR_DESCS_PER_CHANNEL)
  1005. goto out_err;
  1006. cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
  1007. if (!cdesc)
  1008. goto out_err;
  1009. cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
  1010. if (!cdesc->desc)
  1011. goto out_err_alloc;
  1012. for (i = 0; i < periods; i++) {
  1013. desc = dwc_desc_get(dwc);
  1014. if (!desc)
  1015. goto out_err_desc_get;
  1016. switch (direction) {
  1017. case DMA_TO_DEVICE:
  1018. desc->lli.dar = dws->tx_reg;
  1019. desc->lli.sar = buf_addr + (period_len * i);
  1020. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  1021. | DWC_CTLL_DST_WIDTH(reg_width)
  1022. | DWC_CTLL_SRC_WIDTH(reg_width)
  1023. | DWC_CTLL_DST_FIX
  1024. | DWC_CTLL_SRC_INC
  1025. | DWC_CTLL_FC(dws->fc)
  1026. | DWC_CTLL_INT_EN);
  1027. break;
  1028. case DMA_FROM_DEVICE:
  1029. desc->lli.dar = buf_addr + (period_len * i);
  1030. desc->lli.sar = dws->rx_reg;
  1031. desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
  1032. | DWC_CTLL_SRC_WIDTH(reg_width)
  1033. | DWC_CTLL_DST_WIDTH(reg_width)
  1034. | DWC_CTLL_DST_INC
  1035. | DWC_CTLL_SRC_FIX
  1036. | DWC_CTLL_FC(dws->fc)
  1037. | DWC_CTLL_INT_EN);
  1038. break;
  1039. default:
  1040. break;
  1041. }
  1042. desc->lli.ctlhi = (period_len >> reg_width);
  1043. cdesc->desc[i] = desc;
  1044. if (last) {
  1045. last->lli.llp = desc->txd.phys;
  1046. dma_sync_single_for_device(chan2parent(chan),
  1047. last->txd.phys, sizeof(last->lli),
  1048. DMA_TO_DEVICE);
  1049. }
  1050. last = desc;
  1051. }
  1052. /* lets make a cyclic list */
  1053. last->lli.llp = cdesc->desc[0]->txd.phys;
  1054. dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
  1055. sizeof(last->lli), DMA_TO_DEVICE);
  1056. dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
  1057. "period %zu periods %d\n", buf_addr, buf_len,
  1058. period_len, periods);
  1059. cdesc->periods = periods;
  1060. dwc->cdesc = cdesc;
  1061. return cdesc;
  1062. out_err_desc_get:
  1063. while (i--)
  1064. dwc_desc_put(dwc, cdesc->desc[i]);
  1065. out_err_alloc:
  1066. kfree(cdesc);
  1067. out_err:
  1068. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1069. return (struct dw_cyclic_desc *)retval;
  1070. }
  1071. EXPORT_SYMBOL(dw_dma_cyclic_prep);
  1072. /**
  1073. * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
  1074. * @chan: the DMA channel to free
  1075. */
  1076. void dw_dma_cyclic_free(struct dma_chan *chan)
  1077. {
  1078. struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
  1079. struct dw_dma *dw = to_dw_dma(dwc->chan.device);
  1080. struct dw_cyclic_desc *cdesc = dwc->cdesc;
  1081. int i;
  1082. unsigned long flags;
  1083. dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
  1084. if (!cdesc)
  1085. return;
  1086. spin_lock_irqsave(&dwc->lock, flags);
  1087. channel_clear_bit(dw, CH_EN, dwc->mask);
  1088. while (dma_readl(dw, CH_EN) & dwc->mask)
  1089. cpu_relax();
  1090. dma_writel(dw, CLEAR.BLOCK, dwc->mask);
  1091. dma_writel(dw, CLEAR.ERROR, dwc->mask);
  1092. dma_writel(dw, CLEAR.XFER, dwc->mask);
  1093. spin_unlock_irqrestore(&dwc->lock, flags);
  1094. for (i = 0; i < cdesc->periods; i++)
  1095. dwc_desc_put(dwc, cdesc->desc[i]);
  1096. kfree(cdesc->desc);
  1097. kfree(cdesc);
  1098. clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
  1099. }
  1100. EXPORT_SYMBOL(dw_dma_cyclic_free);
  1101. /*----------------------------------------------------------------------*/
  1102. static void dw_dma_off(struct dw_dma *dw)
  1103. {
  1104. dma_writel(dw, CFG, 0);
  1105. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1106. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1107. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1108. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1109. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1110. while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
  1111. cpu_relax();
  1112. }
  1113. static int __init dw_probe(struct platform_device *pdev)
  1114. {
  1115. struct dw_dma_platform_data *pdata;
  1116. struct resource *io;
  1117. struct dw_dma *dw;
  1118. size_t size;
  1119. int irq;
  1120. int err;
  1121. int i;
  1122. pdata = pdev->dev.platform_data;
  1123. if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
  1124. return -EINVAL;
  1125. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1126. if (!io)
  1127. return -EINVAL;
  1128. irq = platform_get_irq(pdev, 0);
  1129. if (irq < 0)
  1130. return irq;
  1131. size = sizeof(struct dw_dma);
  1132. size += pdata->nr_channels * sizeof(struct dw_dma_chan);
  1133. dw = kzalloc(size, GFP_KERNEL);
  1134. if (!dw)
  1135. return -ENOMEM;
  1136. if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
  1137. err = -EBUSY;
  1138. goto err_kfree;
  1139. }
  1140. dw->regs = ioremap(io->start, DW_REGLEN);
  1141. if (!dw->regs) {
  1142. err = -ENOMEM;
  1143. goto err_release_r;
  1144. }
  1145. dw->clk = clk_get(&pdev->dev, "hclk");
  1146. if (IS_ERR(dw->clk)) {
  1147. err = PTR_ERR(dw->clk);
  1148. goto err_clk;
  1149. }
  1150. clk_enable(dw->clk);
  1151. /* force dma off, just in case */
  1152. dw_dma_off(dw);
  1153. err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
  1154. if (err)
  1155. goto err_irq;
  1156. platform_set_drvdata(pdev, dw);
  1157. tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
  1158. dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
  1159. INIT_LIST_HEAD(&dw->dma.channels);
  1160. for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
  1161. struct dw_dma_chan *dwc = &dw->chan[i];
  1162. dwc->chan.device = &dw->dma;
  1163. dwc->chan.cookie = dwc->completed = 1;
  1164. dwc->chan.chan_id = i;
  1165. if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
  1166. list_add_tail(&dwc->chan.device_node,
  1167. &dw->dma.channels);
  1168. else
  1169. list_add(&dwc->chan.device_node, &dw->dma.channels);
  1170. /* 7 is highest priority & 0 is lowest. */
  1171. if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
  1172. dwc->priority = 7 - i;
  1173. else
  1174. dwc->priority = i;
  1175. dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
  1176. spin_lock_init(&dwc->lock);
  1177. dwc->mask = 1 << i;
  1178. INIT_LIST_HEAD(&dwc->active_list);
  1179. INIT_LIST_HEAD(&dwc->queue);
  1180. INIT_LIST_HEAD(&dwc->free_list);
  1181. channel_clear_bit(dw, CH_EN, dwc->mask);
  1182. }
  1183. /* Clear/disable all interrupts on all channels. */
  1184. dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
  1185. dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
  1186. dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
  1187. dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
  1188. dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
  1189. channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
  1190. channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
  1191. channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
  1192. channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
  1193. channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
  1194. dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
  1195. dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
  1196. if (pdata->is_private)
  1197. dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
  1198. dw->dma.dev = &pdev->dev;
  1199. dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
  1200. dw->dma.device_free_chan_resources = dwc_free_chan_resources;
  1201. dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
  1202. dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
  1203. dw->dma.device_control = dwc_control;
  1204. dw->dma.device_tx_status = dwc_tx_status;
  1205. dw->dma.device_issue_pending = dwc_issue_pending;
  1206. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1207. printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
  1208. dev_name(&pdev->dev), dw->dma.chancnt);
  1209. dma_async_device_register(&dw->dma);
  1210. return 0;
  1211. err_irq:
  1212. clk_disable(dw->clk);
  1213. clk_put(dw->clk);
  1214. err_clk:
  1215. iounmap(dw->regs);
  1216. dw->regs = NULL;
  1217. err_release_r:
  1218. release_resource(io);
  1219. err_kfree:
  1220. kfree(dw);
  1221. return err;
  1222. }
  1223. static int __exit dw_remove(struct platform_device *pdev)
  1224. {
  1225. struct dw_dma *dw = platform_get_drvdata(pdev);
  1226. struct dw_dma_chan *dwc, *_dwc;
  1227. struct resource *io;
  1228. dw_dma_off(dw);
  1229. dma_async_device_unregister(&dw->dma);
  1230. free_irq(platform_get_irq(pdev, 0), dw);
  1231. tasklet_kill(&dw->tasklet);
  1232. list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
  1233. chan.device_node) {
  1234. list_del(&dwc->chan.device_node);
  1235. channel_clear_bit(dw, CH_EN, dwc->mask);
  1236. }
  1237. clk_disable(dw->clk);
  1238. clk_put(dw->clk);
  1239. iounmap(dw->regs);
  1240. dw->regs = NULL;
  1241. io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1242. release_mem_region(io->start, DW_REGLEN);
  1243. kfree(dw);
  1244. return 0;
  1245. }
  1246. static void dw_shutdown(struct platform_device *pdev)
  1247. {
  1248. struct dw_dma *dw = platform_get_drvdata(pdev);
  1249. dw_dma_off(platform_get_drvdata(pdev));
  1250. clk_disable(dw->clk);
  1251. }
  1252. static int dw_suspend_noirq(struct device *dev)
  1253. {
  1254. struct platform_device *pdev = to_platform_device(dev);
  1255. struct dw_dma *dw = platform_get_drvdata(pdev);
  1256. dw_dma_off(platform_get_drvdata(pdev));
  1257. clk_disable(dw->clk);
  1258. return 0;
  1259. }
  1260. static int dw_resume_noirq(struct device *dev)
  1261. {
  1262. struct platform_device *pdev = to_platform_device(dev);
  1263. struct dw_dma *dw = platform_get_drvdata(pdev);
  1264. clk_enable(dw->clk);
  1265. dma_writel(dw, CFG, DW_CFG_DMA_EN);
  1266. return 0;
  1267. }
  1268. static const struct dev_pm_ops dw_dev_pm_ops = {
  1269. .suspend_noirq = dw_suspend_noirq,
  1270. .resume_noirq = dw_resume_noirq,
  1271. };
  1272. static struct platform_driver dw_driver = {
  1273. .remove = __exit_p(dw_remove),
  1274. .shutdown = dw_shutdown,
  1275. .driver = {
  1276. .name = "dw_dmac",
  1277. .pm = &dw_dev_pm_ops,
  1278. },
  1279. };
  1280. static int __init dw_init(void)
  1281. {
  1282. return platform_driver_probe(&dw_driver, dw_probe);
  1283. }
  1284. subsys_initcall(dw_init);
  1285. static void __exit dw_exit(void)
  1286. {
  1287. platform_driver_unregister(&dw_driver);
  1288. }
  1289. module_exit(dw_exit);
  1290. MODULE_LICENSE("GPL v2");
  1291. MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
  1292. MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
  1293. MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");