dw_dmac.c 38 KB

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