dw_dmac.c 42 KB

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