dw_dmac.c 38 KB

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